package number1;
//数组的查找操作
public class Test7 {
public static void main(String[] orgs){
int[] arr={1,5,7,8,13,15,18};
int index=halfSearch(arr,13);
System.out.println("index="+index);
}
public static int halfSearch(int[] arr,int key)
{
int min,max,mid;
min=0;
max=arr.length-1;
mid=(min+max)/2;
while(arr[mid]!=key){
if(key>arr[mid])
min=mid+1;
else if(key<arr[mid])
max=mid-1;
if(min>max)
return -1;
mid=(min+mid)/2;
}
return mid;
}
}
|