import java.util.Scanner;
public class 第六次二分查找法 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("输入数组长度");
int num = sc.nextInt();
int a[] = new int[num];
System.out.println("按从小到大的顺序输入数组(不能查询-666)");
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
System.out.println("输入要查找的数字:");
int number = sc.nextInt();
int k = binarySearch(a, number);
if (k == -666)
System.out.println("查无此数");
else
System.out.println("确有此数");
}
public static int binarySearch(int[] a, int key) {
int f = 0, e = a.length - 1;
int result = -666;
while (f < e) {
int z = (f + e) / 2;
if (a[z] == key) {
return key;
} else if (a[z] < key) {
e = z;
} else {
f = z;
}
}
return result;
}
}