public class HalfSreach {
public static int halfSearch(int[] arr,int key)
{
int min,max,mid;
min=0;
max=arr.length-1;
mid=(min+max)/2;
while(key!=arr[mid])
{
if(key>arr[mid])
min=mid+1;
else if(key<arr[mid])
max=mid-1;
mid=(min+max)/2;
if(min>max)
return -1;
}
return mid;
}
public static void main(String[] args) {
int[] arr={1,6,9,10,61};
System.out.println(halfSearch_1(arr, 6));
}
}
如果把else去掉,结果就不对了,为什么?
|