二分查找:
int binary_search_recursion(const int array[], int low, int high, int key) { int mid = low + (high - low)/2; if(low > high) return -1; else{ if(array[mid] == key) return mid; else if(array[mid] > key) return binary_search_recursion(array, low, mid-1, key); else return binary_search_recursion(array, mid+1, high, key); } }
int binary_search_loop(const int array[], int len, int key) { int low = 0; int high = len - 1; int mid; while(low <= high){ mid = (low+high) / 2; if(array[mid] == key) return mid; else if(array[mid] > key) high = mid - 1; else low = mid + 1; } return -1; }