1 int binarySearch(int a[], int size, int p) { 2 int L = 0;//左端点 3 int R = size - 1;//右端点 4 int result = -1; 5 while( L <= R) { 6 int C = L + (R - L)/2;//为了防止L+R过大,溢出 7 if(p < a[C]) { 8 R = C - 1; 9 } else if(p > a[L]) { 10 L = C + 1; 11 }else { 12 result = C; 13 break; 14 } 15 } 16 return result; 17 }