import java.util.*;
class HalfSearch
{
public static void main(String[] args)
{
int[] arr = {2,5,63,2,4,0,6,5};
Arrays.sort(arr);
for(int a:arr)
{
System.out.print(a+" ");
}
System.out.println();
int point = search(arr,3);
System.out.println(point);
}
public static int search(int[] a,int num)
{
int min=0;
int max = a.length-1;
int mid = (min+max)>>1;
while(min <= max)
{
if(num< a[mid])
{
max = mid-1;
}
else if(num > a[mid])
{
min = mid +1;
}
else
return mid;
mid = (min+max)>>1;
}
return -min-1;
}
}
|
|