Thursday 17 January 2013

Binary Search C plus plus program

| |


#include<iostream>


using namespace std;
int main()
{

int a[10],n,beg,end,mid,x;
cout<<"\nNo of element in array\t";
cin>>n;
cout<<"enter the element\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nenter item to be searched\t";
cin>>x;
beg=0;end=n;
mid=(beg+end)/2;
while(beg<=end&&a[mid]!=x)
{
if(x<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}
if(a[mid]==x)
{
cout<<"\nItem found at location "<<mid+1;
}
else
{
cout<<"\nItem not found";
}
system ("pause");
return 0;
}


Binary Search C plus plus program


Related Posts Plugin for WordPress, Blogger...
Powered by Blogger.