class ArrayDemo8
{
public static void main(String[] args)
{
int[] arr = {1,3,8,9,45,56,76};
int index = halfSearch(arr,45);
System.out.println("index="+index);
}
public static int halfSearch(int[] arr,int key)
{
int min,mid,max;
min=0;
max=arr.length-1;
mid=(max+min)/2;
while(arr[mid]!=key)
{
if(key>arr[mid])
min = mid+1;
else if(key<arr[mid])
max = mid-1;
if(max<min)
return -1;
mid = (max+min)/2;
}
return mid;