本帖最后由 rel4x 于 2014-9-16 20:08 编辑
class HalfsearchDemo
{
public static void main(String[] args)
{
int [] arr = new int [] {1,3,4,6,9,10};
halfsearch(arr,50);
}
public static void halfsearch(int[] arr, int key)
{
int max,min,mid;
max=arr.length-1;
min=0;
mid=(max+min)>>1;
while (arr[mid]!=key)
{
if (key>arr[mid])
min=mid+1;
else if (key<arr[mid])
max=mid-1;
if (max<min)
System.out.println("非法");
mid = (max+min)>>1;
}
System.out.println(mid);
}
}
|
|