二分查找
package sort;
/**
* Create by ~JH~ on 2018/4/30
*/
public class TwoQuery {
public static void main(String[] args) {
int a[]={0,21,33,33,41,59,68,75,76,77};
System.out.println(TwoQuery.twoQuery(a,0,a.length,33));
}
private static int twoQuery(int[] a,int low,int high,int n) {
while (low < high) {
int mid = (low + high) / 2;
if (a[mid] > n) {
high = mid - 1;
}else if (a[mid]==n){
return mid;
}else{
low=mid+1;
}
System.out.println("l:"+low+" h:"+high);
}
return -1;
}
}