Serach

时间:2022-01-22 01:03:00

1.二分查找

 public class BubbleSort {
public static int binarySerach(int[] a,int value){
int low=0;
int high=a.length-1;
int min=(low+high)/2;
while(low<=high){
if(value==a[min]){
return min;
}
if(value>a[min]) low=min+1;
else high=min-1;
min=(low+high)/2;
}
return -1;
} public static void main(String[] args){
int[] a={1,3,5,6,7,8,11,15,19,22,23};
int value=1;
int i=binarySerach(a,value);
//System.out.println(i);
if(i>0) System.out.println(value+"是"+i+"个数字");
else System.out.println("找不到这个数字"); }
}