二分查找
/**
* 二分查找
*/
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {12, 34, 45, 56, 67, 78, 89, 90, 99};
System.out.println(getIndex(arr, 99));
}
public static int getIndex(int[] arr, int value){
int low = 0;
int high = arr.length-1;
while(low<=high){
int mid = (low + high)/2;
if(arr[mid]>value){
high = mid - 1;
}else if (arr[mid]<value){
low = mid + 1;
}else{
return mid;
}
}
return -(low+1);
}
}