botao555
你说的是二分法查找数组中某个元素的下标吧public class Binary {
public static void main(String[] args) {
int[] arr = {1,2,3,5,8};
System.out.println(binaryFind(arr,8));
}
public static int binaryFind(int[] arr,int value){
int low=0,high=arr.length - 1;
while(low <= high){
int midle = (low + high) / 2;
midle = (low + high) / 2;
if(arr[midle] == value){
return midle;
}
if(arr[midle] > value){
high = midle + 1;
}else{
low = midle + 1;
}
}
return -1;
}
}