Wednesday, September 17, 2008

BINARY SEARCH in c programming language

56. BINARY SEARCH

void main()
{
int a[10],i,n,m,c=0,l,u,mid;
clrscr();
printf("Enter the size of an array->");
scanf("%d",&n);
printf("\nEnter the elements of the array->");
for(i=0;i
{
scanf("%d",&a[i]);
}
printf("\nThe elements of an array are->");
for(i=0;i
{
printf(" %d",a[i]);
}
printf("\nEnter the number to be search->");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
break;
}
else
if(m
{
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("\nThe number is not in the list");
else
printf("\nThe number is found");
getch();
}

No comments:

Post a Comment

Thanks to given comments.......

My Blog List