A C program for Binary search using array

#include<stdio.h>
int binarysearch(int a,int m,int n)
{
int u,mid,h;
h=0;
u=n-1;
{
while(1<=u)
{
mid=(h+u)/2;
if(m==a[mid])
{
c=1;
break;
}
else if(m<a[mid])
{
u=mid-1;
}
else
h=mid+1;
}
return mid;
}

void main()
{
int a,i,n,m,p;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements in ascending order");
for(i=0;i<n;i++)
{
scanf("%d",a(a+i));
}
printf("enter the number to be search");
scanf("%d",&m);
p=binarysearch(a,m,n);
if(c==0)
printf("the number is not found");
else
printf("the number is found at position %d\n",p+1);
getch();
clrscr();
}

Comments

Popular posts from this blog

Visti Cloud Service (VCS)

Being a DARK SKINNED is not a PROBLEM