Skip to content

Instantly share code, notes, and snippets.

@longbuilder
Created November 4, 2014 01:50
Show Gist options
  • Save longbuilder/fb97385cb2eaf5681cdc to your computer and use it in GitHub Desktop.
Save longbuilder/fb97385cb2eaf5681cdc to your computer and use it in GitHub Desktop.
binary_search
// 返回最后一个不大于 target 的位置
template <typename T>
int binary_search(T *A, int low, int high, T target)
{
while (low < high) {
int mid = (low + high) / 2;
target < A[mid] ? (high = mid) : (low = mid + 1);
}
return --low;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment