本帖最后由 赵学刚 于 2012-12-3 11:20 编辑
public class test5 {
public static int halfSearch(int b [],int key){
int max=b.length-1,min=0,mid;
while(min<=max){
mid=(max+min)>>1;
if(key>b[mid]){
mid=mid+1;
}else if(key<b[mid]){
max=mid-1;
}else{
return mid;
}
}
return -1;
}
public static void main(String[] args) {
int [] arr =new int[]{2,5,6,7,9,1};
int index=halfSearch(arr,1);
System.out.println("index="+index);
}
}
为什么输出是-1呢,而key=2确实正确的结果,各位帮我看看哪里出问题了
|