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);
}
再有就是折半查找的前提是有序。作者: 潘天功 时间: 2012-11-24 22:49
你代码写的不对,而且不规范,我把你的改了改,你看看吧
public static int halfSearch(int[] b,int key)
{
int min = 0;
int max = b.length-1;
int mid = (min+max)/2;