基本方法
private static int rank(int key,int[] a){
int lo=0;
int hi=a.length;
while(lo<=hi){
//要查找的键要么不存在要么必然存在于a[lo....hi]d当中
int mid=lo+(hi-lo)/2;
if(key<a[mid]){
hi=mid-1;
}else if(key>a[mid]){
lo=mid+1;
}else
return mid;
}
return -1;
}
调用
int[] xx={3,4,9,33,41,9,89,34,49,22,45,54,53,54,64,12,34,68,35,23,545,43,23,464,4,34,2,64,45,75,86,32};
for(int i:xx){
System.out.print(i+"--");
}
Arrays.sort(xx);
System.out.println();
for(int i:xx){
System.out.print(i+"--");
}
System.out.println("\n"+rank(45,xx)+"++++");