C implementation of binary Search

Binary Search C Implementation      Binary Search C++ Implementation

/* binary search the numbers should be sorted */

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,pos,x;
int first,last,middle;
clrscr();
printf(“enter the size of the array “);
scanf(“%d”,&n);
printf(“enter the elements of the array in sorted order\n”);
for(i=0;i<n;i++)
{
printf(“enter the %d element “,i);
scanf(“%d”,&a[i]);
}
printf(“enter the element to search “);
scanf(“%d”,&x);
/* to search the array */
first=0;
pos=-1;
last=n-1;
while((first<=last) && (pos==-1))
{
middle=(first+last)/2;
if (a[middle]==x)
pos=middle+1;
else
if (x>a[middle])
first=middle+1;
else
last=middle-1;
}
if (pos>-1)
printf(“the element is found at %d position\n”,pos);
else
printf(“the element is not found\n”);
getch();
}

Binary Search C Implementation      Binary Search C++ Implementation