Binary Search Array C++ language Program Code

C++ – Binary Search Array C language Program Code

#include
#include
void main()
{
int a[20],n,i,top,bottom,mid,f,s;
clrscr();
cout<<"Enter the value of n:";
cin>>n;
cout<<"nEnter the values:n";
for(i=0;i {
cin>>a[i];
}
cout<<"Enter the element to be searched:";
cin>>s;
top=0;
bottom=n-1;
f=0;
while(top<=bottom && f==0)
{
mid=(top+bottom)/2;
if(s>a[mid])
{
top=mid+1;
}
else if(s {
bottom=mid-1;
}
else
f=1;
}
if(f==1)
cout<<"Element found";
else
cout<<"Element not found.";
getch();

}

Add Comment