Created
November 4, 2014 01:50
-
-
Save longbuilder/fb97385cb2eaf5681cdc to your computer and use it in GitHub Desktop.
binary_search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 返回最后一个不大于 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