// 折半查找的前提是数据必须有序
public static int halfSeek(int [] array, int target) {
int start = 0;
int end = array.length - 1;
int mid = 0;
while (start <= end) {
mid = (start + end) / 2;
if (array[mid] > target) {
end = mid - 1;
} else if (array[mid] < target) {
start = mid + 1;
} else {
return mid;
}
}
return -1;
}
java折半查找(二分法)
最新推荐文章于 2022-07-21 16:05:49 发布