I hope you have read this page: http://terminalcoders.blogspot.com/2014/07/searching.html. If you didn't read it, please read it before using the source code.
#include <iostream>
using namespace std;
class BIN
{
int list[256], n, i, k;
public:
void read()
{
cout<<"Enter the number of items: ";
cin>>n;
cout<<"Enter "<<n<<" Items in ascending order: ";
for(i=0;i<n;i++)
{
cin>>list[i];
}
cout<<"Enter key to search: ";
cin>>k;
binary(0, n);
}
void binary(int f, int l)
{
if(f>l)
{
cout<<"Key hasn't found!!!";
}
else
{
int mid = (f+l)/2;
if(k==list[mid])
{
cout<<"Key has found!!!";
}
else if(k < list[mid])
{
binary(f, mid-1);
}
else
{
binary(mid+1, l);
}
}
}
};
int main()
{
BIN o;
o.read();
}
