3. 二分法查找
public static void main(String[] args) {
int[] arr={11,22,33,40,44,55,66};
System.out.println(erFen(arr, 33));
}
public static int erFen(int[] arr,int value){
int max=arr.length-1;
int min=0;
int mid=(max+min)/2;
while(arr[mid]!=value){
if(arr[mid]>value){
max=mid-1;
}
else if (arr[mid]<value) {
min=mid+1;
}
mid=(max+min)/2;
if(min>max){
return -1;
}
}
return mid;
}
} |