package search;
public class ErfenSearch {
public static void main(String[] args) {
// 二分查找的前提数组必须有序
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
erfenSearch(array, 2);
}
public static void erfenSearch(int[] a, int n) {
int min = 0;
int max = a.length - 1;
while (min <= max) {
int mid = (min + max) / 2;
if (a[mid] == n) {
System.out.println("查找成功");
return;
}
if (a[mid] < n) {
min = mid + 1;
}
if (a[mid] > n) {
max = mid - 1;
}
}
System.out.println("查找失败");
}
}
二分查找(Java)
最新推荐文章于 2024-11-03 11:01:20 发布