class HalfFind
{
public static void main(String[] args)
{
int[] arr = {2,3,4,5,6,7,8,9};
System.out.println(find(arr,1));
}
static int find(int[]arr,int value)
{
int min =0;
int max =arr.length-1;
int mid =(min+max)/2;
if(arr[mid]!=value) {
if (value > arr[mid])
{
min =mid+1;
}
else if ((value < arr[mid]))
{
max=mid-1;
}
if (min>max)
{
return -1;
}
mid =(min+max)/2;
}
return mid;
}
} |
|