1 int binary_search(int* a, int len, int goal) 2 { 3 int low = 0; 4 int high = len - 1; 5 while(low <= high) 6 { 7 int middle = (low + high)/2; 8 if(a[middle] == goal) 9 return middle;10 //在左半边11 else if(a[middle] > goal)12 high = middle - 1;13 //在右半边14 else15 low = middle + 1;16 }17 //没找到18 return -1;19 }