public class BinarySearch {
public static int binarySearch(int[] array, int param) {
int low = 0;
int high = array.length -1;
while(low <= high) {
// >>> 无符号右移运算符,此处相当于/2
int mid = (low + high) >>> 1;
if(array[mid] == param) {
return mid;
} else if(array[mid] < param) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
}
二分查找法
最新推荐文章于 2019-01-21 16:03:18 发布