标签:
二分法查找,前提是已经有序
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 else 15 low = middle + 1; 16 } 17 //没找到 18 return -1; 19 }
标签:
原文地址:http://www.cnblogs.com/jack-xu/p/4832813.html