A blog to help students to learn programming in various languages including C, C++, Java, Python and many more.

Subscribe For Free Latest Updates!

We'll not spam mate! We promise.

Saturday, 23 January 2016



Program Screenshot
Output

#include <iostream.h>
using namespace std;
int a[100];
int main()
{
int n,item,loc;
int binary(int beg,int end,int item);
cout<<"Enter no. of elements: ";
cin>>n;
cout<<"Enter "<<n<<" elements in Sorted Order: ";
for(int i=0;i<n;i++) {
cin>>a[i];
}
cout<<"Enter item you want to search: ";
cin>>item;
loc=binary(0,n,item);
if(a[loc]==item) {
cout<<"\nData is Found at Location: "<<loc<<"\n";
} else {
cout<<"Data is Not Found";
}
}
int binary(int beg,int end,int item)
{
int mid=(beg+end)/2;
if (end < beg) {
    return -1;
}
if (a[mid]==item) {
return(mid);
} else if(a[mid]<item) {
binary(mid+1,end,item);
} else {
binary(beg,mid-1,item);
}
}

0 comments :

Post a Comment



Copyright © 2016 - ProgrmIt - All Rights Reserved
(Articles Cannot Be Reproduced Without Author Permission.)
Design By : | Powered By: Blogger