Docstoc

pro12

Document Sample
pro12 Powered By Docstoc
					#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
class binary_search
{
  private :
     int datas[10];
  public :
     int size;
     binary_search()
     { int k;
         clrscr();
         size=10;
         for(int i=0;i<size;i++)
         {
         cout << "Enter the value for item " << i+1 << " : ";
         cin >> datas[i];
         }
         for(i=0;i<size-1;i++)
         {
       for(int j=i+1;j<size;j++)
       {
         if(datas[i]>datas[j])
         {
             k=datas[i];
             datas[i]=datas[j];
             datas[j]=k;
         }
       }
         }
       cout<<endl;
         for(i=0;i<size;i++)
       {
           cout <<i+1 << " ->" << datas[i]<<endl;
       }
   }
   void search()
   {
     int item,found,mid,lb,ub;
     ub=size-1;found=0;lb=0;
     cout<<endl;
     cout<<"Enter the value you want to search : ";
     cin>>item;
     while(found==0 && lb <= ub)
     {
       mid=(ub+lb)/2;
       if(datas[mid]==item)
       {
       found=1;
       }
       else
       {
         if(datas[mid]<item)
         {
     lb=mid+1;
      }
      else
      {
     ub=mid-1;
      }
     }
     }
     cout<<endl;
     if(found==1)
     {
       cout<<"Data you searched " << item << " is found at position " <<
mid+1 << ".";
     }
     else
     {
       cout<<"Data you searched " << item << " is not found.";
     }
     cout<<endl;
  }
  };
  void main()
  {
   char ch;
   binary_search bs1;
   do
   {
     bs1.search();
     cout<<endl;
     cout << "Do you want to search any more data (Y/N) : ";
     ch=getch();
   }while(ch != 'N' && ch != 'n');
}

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:10/18/2012
language:Unknown
pages:2