Sunday, 2 July 2017

C++ program of binary search

#include<iostream>
#include<conio.h>
using namespace std;
int a[10];
void binary(int p, int r, int data)
{
        
         int q;
         q=(p+r)/2;
        
         if(a[q]==data)
         {
                 cout<<"found";
         }
         else if(data<a[q])
         {
                 binary(p,q-1,data);
         }
         else
         {
                 binary(q+1,r,data);
         }
        
}

int main()
{
         int d;

        
         for(int i=0;i<10;i++)
         {
                 cout<<"Enter data at ["<<i<<"] : ";
                 cin>>a[i];
         }
         cout<<"Enter data to find : ";
         cin>>d;
        
         binary(0,9,d);
}


OUTPUT:
Share:

0 comments:

Post a Comment