Now question is how to write efficient algoritham so that we can reduce 
the number of comparison.In Normal binary search takes 2logN comparison 
but how to write code so that binary search can be done 
in logN+2 comparison.
Normal Binary search
while(first<last)
{
    mid=(first+last)/2;
    if(x==L[mid])
    {
        index=mid;
        break;
    }
    else if(x>L[mid])
    {
        first=mid+1;
    }
    else
    {
        last=mid-1;
    }
}

In the above case it will take 2logN comparison.
But we can do it in logN + 2 comparisons. Please cheek the below code .

Here is the most efficient binary search algo or code in c language.
	while(first<last-1)
	{
		mid=(first+last)/2;
		if(x>=L[mid])
		{
			first = mid;
		}
		else
		{
			last=mid-1;
		}

	}
#include<stdio.h>
void main()
{
	int L[10];
	int first,last,index,mid,x;
	L[1]=3;
	L[2]=5;
	L[3]=6;
	L[4]=7;
	L[5]=8;
	L[6]=9;
	index=0;
	printf("Give the number to search\n");
	scanf("%d",&x);
	first=1;
	last=6;
	while(first<last-1)
	{
		mid=(first+last)/2;
		if(x>=L[mid])
		{
			first = mid;
		}
		else
		{
			last=mid-1;
		}

	}

	if(x==L[first])
		index=first;
	if(x==L[last])
		index=last;

	printf("The number %d is in %d th position\n",x,index);
}


Also see 

Find the position of smallest element in Rotated Sorted Array 


Question

1)What is the Pre condition of Binary Search? Ans:-The input array should be sorted. The Binary search can be applied if the array is in ascending or descending order.
Free Web Hosting