public class arrDemo{
public static int halfselect(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=min+1;
else if(key>arr[mid])
max=max-1;
if(arr[min]>arr[max])
return -1;
}
return mid;
}
public static void main(String[] args){
int[] x={1,2,3,4,5,35};
int y=5;
halfselect(x,y);
}
}
|