Binary Search Program in CPP(C++)

 This program contains some errors you need to remove.
___________________________________________________

#include<iostream>
using namespace std;

class BinarySearch
{
int i;
int n;
public:
void b_search(int *, int, int);
};

void BinarySearch :: b_search(int a[],int n, int dataitem)
{
int start, end, mid, index, f=0;
start=0;
end = n-1;
cout<<“Enter data item to be searched”;
while( (start <= end) && ( dataitem !=a[mid]))
{
mid = (( start + end)/2);
if(dataitem == a[mid])
{
cout<<“Position of the data item is”<< mid+1;
f=1;
} else if( dataitem < a[mid])
{
end = mid -1;
}  else
start=mid +1;
if(f==0){
cout<<” Search is not successful”;
}
}

int main()
{
int a[8], n, dataitem, int i;
BinarySearch bs;
cout<<“Enter number of items to be inserted”;
cin>>n;
cout<<” Enter data items”;
for(i=0 ; i<=n; i++)
{
cin>> a[i];
}
cout<<” Enter data item to be searched”;
cin>> dataitem;
bs.b_search(a,n,dataitem);

return 0;
}

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.