C program to Implement Binary search

Implement Binary search Algorithm for an array


#include<stdio.h>
#include<conio.h>
int a[20];
void search(int *a,int n)
{
int i,item,flag=0,start=0,mid,end=n-1;
printf("\nEnter the element to be searched");
scanf("%d",&item);
for(i=0;i<n;i++)
{
mid=(start+end)/2;
if(item==a[i])
{
printf("\nElement %d is found in %d position",item,i+1);
flag=1;
break;
}
else if(item<a[mid])
end=mid-1;
else if(item>a[mid])
start=mid+1;
}
if (flag!=1)
printf("\n!Not found");
}
void sort(int *a,int n)
{
int i,j,tmp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(a[j]<a[i])
{
tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
}
}
void main()
{
int n,i;
char c;
clrscr();
printf("\nEnter the limit");
scanf("%d",&n);
printf("\nEnter numbers");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe array elements are:\t");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\nThe sorted array is:\t");
sort(&a,n);
for(i=0;i<n;i++)
printf("\t%d",a[i]);
do
{
search(&a,n);
printf("\nPress y for search another number");
c=getche();
}while(c=='y'||c=='Y');
getch();
}