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;
}
网友评论