Friday, 7 December 2012

Linear search and binary search


Program to search a number in a list using linear or binary search.

Implement binary and linear search


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

const int MAX=50;

class array
{ private:
          int arr[MAX];
           int count;
 public:
           array()
          {count=0;}
          void add(int);
          void display(void);
          int binarysearch(int);
          int linearsearch(int);
          void sort(void);
};

void array::add(int item)
{        if(count<MAX)
          {        arr[count]=item;
                   count++;
           }
           else
                   cout<<"\nArray is full"<<endl;
}
void array::display(void)

{        cout<<"There are"<<count<<" elements in the list"<<endl;
          cout<<"They are:"<<endl<<endl;
          for(int i=0;i<count;i++)
                    cout<<arr[i]<<" ";
}
void array::sort(void)
{        for(int i=0;i<count-1;i++)
                   for(int j=0;j<(count-i-1);j++)
                             if(arr[j]>arr[j+1])
                             {        int t=arr[j];
                                      arr[j]=arr[j+1];
                                      arr[j+1]=t;
                             }
          return;
}
int array::binarysearch(int num)
{        int mid,lower=0,upper=count-1;
          for(mid=(lower+upper)/2;lower<=upper;
                   mid=(lower+upper)/2)
                   {        if(arr[mid]==num)
                              return mid;
                             if(arr[mid]>num)
                                      upper=mid-1;
                             else
                                      lower=mid+1;
                   }
          return-1;
}
int array::linearsearch(int num)
{        for(int i=0;i<count;i++)
                   if(arr[i]==num)
                             return i;
return -1;
}
void main()
{        int element,n,i,c;
          array b;
          while(1)
          {        clrscr();
                   cout<<"Perform the Binary and linear search \n";
                   cout<<"_________________________"<<endl;
                   cout<<"1.Input data into the list \n";
                   cout<<"2.Display data in the list \n";
                   cout<<"3. search an item in the list using Binarysearch\n";
                   cout<<"4. search an item in the list using Linearsearch\n";
cout<<"5.Exit from program \n";
                   do
                   {        cout<<"Select Your Choice(1:5):";
                             cin>>c;
                             cout<<"";
                   }
                   while((c<1)||(c>5));
          if(c==5)exit(1);
          clrscr();
          switch(c)
          { case 1:
                   clrscr();
                   cout<<"How many elements:";
                   cin>>n;
                   cout<<endl<<endl;
                   for(i=0;i<n;i++)
                   {        cout<<"Enter number"<<i+1<<":";
                             cin>>element;
                             b.add(element);
                   }
                   break;
      case 2:
                   clrscr();
                   b.sort();
                   b.display();
                   break;
      case 3:
                   clrscr();
                   b.sort();
                   cout<<"Enter number to binarysearch:";
                   cin>>element;
                   i=b.binarysearch(element);
                   if(i==-1)
                             cout<<"Number is not present in the array.";
                   else
cout<<"The element"<<element<<"is at position"<<i+1<<"in the list.";
                    cout<<endl;
                    break;
case 4 :
                   clrscr();
                   b.sort();
                   cout<<"Enter number to search:";
                   cin>>element;
                   i=b.linearsearch(element);
                   if(i==-1)
                               cout<<"Number is not present in the array.";
                   else
cout<<"The element"<<element<<"is at position"<<i+1<<"in the list.";
                    cout<<endl;
                    break;
}
          cout<<endl<<"Press Any Key to Continue...";
          getch();
    }
}

No comments:

Post a Comment