import java.util.Arrays;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("输入要查询的数");
int[] ary= {56,41,411,65,12,77,2,71,92,65,50,78};
int n=input.nextInt();
int a=0,b=ary.length;
int x;
Arrays.sort(ary);
while(true) {
x=(a+b)/2;
if(n==ary[x]) {
System.out.println("在第"+(x+1)+"个查找到");
break;
}else if(a==b) {
System.out.println("查无此数");
break;
}
else if(n<ary[x]) {
b=x;
}
else if(n>ary[x]) {
a=x;
}
}
}
}
二分法查找一维数组元素位置
最新推荐文章于 2024-07-25 15:58:50 发布