C++ 基础算法之二分查找
前提:
有序数组!
int binary_search(int* a, int len, int goal) { int low = 0; int high = len - 1; while(low <= high) { int middle = (low + high)/2; if(a[middle] == goal) return middle; //在左半边 else if(a[middle] > goal) high = middle - 1; //在右半边 else low = middle + 1; } //没找到 return -1; }