/**
* 二分法查找
* 二分法查找建立在已经排序好的基础上
*/
public class BinaryChopTest {
public static void main(String[] args) {
int []arry = {1,3,4,5,8,10,15,25,29};
Scanner in = new Scanner(System.in);
System.out.println("请输入您要查找的元素:");
int num = in.nextInt();
System.out.println(binarySearch(arry,num));
}
public static String binarySearch(int [] a,int num){
int begin = 0;
int end = a.length-1;
while(begin<=end){
int mid = (begin+end)/2;
if(a[mid]==num){
return "您输入的数的下标是:"+mid;
}
if(a[mid]>num){
end = mid-1;
}
if(a[mid]<num){
begin = mid+1;
}
}
return "您输入的数不在当前数组中";
}
}
最后那两个if啥意思不太懂
芝麻大点