package test;
public class SplitBy2 {
static int num = 12;
static int[] arr = { 1, 3, 7, 11, 12, 17, 23, 25, 27 };
static int start, end, index;
public static void main(String[] args) {
System.out.println(splitBy2(arr, num));
}
public static int splitBy2(int arr[], int num) {
start = 0;
end = arr.length - 1;//8
while (true) {
index = (start + end) / 2;
if (arr[index] == num) {
return index;
} else if (start > end) {
System.out.println("没有找到");
return -1;
} else {
if (arr[index] > num) {
end = index - 1;
}
if (arr[index] < num) {
start = index + 1;
}
}
}
}
}
java模拟二分法
最新推荐文章于 2021-08-02 07:45:20 发布