class Test3 {
public static void main(String[] args)
{
int [] arr ={1,3,5,7,11,13,14,16,17};
int x = get(arr,9);
System.out.println(x);
}
public static int get(int [] arr,int key) {
int min=0,max=arr.length-1,mid;
while(min<=max){
mid = (min+max)/2;
if(key>arr[mid])
min = mid+1;
else if (key<arr[mid])
max = mid-1;
else
return mid;
}
return min;
}
} |
|