public static int binarySearch(int[] x,int key) {
int start = 0;
int end = x.length-1;
while(true) {
int mid = (start + end)/2;
if(x[mid]>key) {
end = mid - 1;
}
if(x[mid]<key) {
start = mid +1;
}
if(x[mid]==key) return mid;
if(start>mid) return -1;
}
}
api 中有java自己实现的二分查找方法
Array.binarySearch();