递归折半查找
#include<iostream>
using namespace std;
int FindX(int*list, int left, int right, int X);
int main(){
int a[12] = { 1, 2, 5, 7, 12, 34, 35, 46, 57, 58, 67, 78 };
cout << FindX(a, 0, 11,67) << endl;
return 0;
}
int FindX(int*list, int left, int right, int X){
/*找到就返回下标,否则返回-1*/
if (right > left)
return -1;
int center = (right+left) / 2;
if (list[center] == X)
return center;
else if (X < list[center])
FindX(list, left, center - 1, X);
else{
FindX(list, center + 1, right, X);
}
}