C++实现二分查找算法的源码
如下代码内容是关于C++实现二分查找算法的的代码。
<<array binary search>>=
template< typename T, typename compare_less >
int array_binary_search(T a[], int low, int high, T target) {
while (low <= high) {
int middle = low + (high - low)/2;
if (compare_less(target, a[middle]))
high = middle - 1;
else if (compare_less(a[middle], target))
low = middle + 1;
else
return middle;
}
return -1;
}