write a program to find the elements of an array by using binary search .

#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],first=0,middle=0,last,search,n,i;
printf("enter the size of an array\n");
scanf("%d",&n);
printf("enter integer in ascending order\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter value to search\n");
scanf("%d",&search);
last=n-1;
while(first<=last)
{
middle=(first+last)/2;
if(a[middle]<search)
{
first=middle+1;
}
else if(a[middle]==search)
{
printf("%d found at location %d\n",search,middle+1);
break;
}
else
{
last=middle-1;
}
}
if(first>last)
{
printf("%d not found\n",search);
}
return 0;
}

Comments

Popular posts from this blog

write a program for insertion sort with example.