<<算法导论>>学习之二分查找
2018-03-17 本文已影响18人
孙金亮
int methodFirst(){
int key = 3;
int array[] = {1,3,5,7,9};
int L = 0;
int R = sizeof(array)/ sizeof(array[0]) - 1;
while (L <= R) {
int M = L + (R - L)/2;
if(key < array[M]){
R = M - 1;
}else if (key > array[M]){
L = M + 1;
}else{
return M;
}
}
return -1;
}