public int find(long searchKey){ int i;
int begin = 0;
int end = nElems - 1; while(true){
i = (begin + end) / 2; if (searchKey == a[i]){
return i;
} else if (begin > end) {
return -1;
} else if (searchKey > a[i]) {
begin = i + 1;
} else if (searchKey < a[i]) {
end = i - 1;
}
}
}
其中a是待查有序数组,searchKey是待查数值。
查询次数为num = log2(a.length)即数组长度的对数