Pages

Monday, 8 November 2021

Program to illustrate Binary Search in c++

 /*Program to illustrate Binary Search Program */

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class Bisearch
{
  public:
  int i,j,temp,a[10],search,n;
  public:
  void getdata();
  int  searching();
  void sort();
  void display();
};
void Bisearch::getdata()
{
cout<<"Enter the size of the array you want to create:--  "<<endl;
cin>>n;
cout<<"Enter the elements"<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Enter your search Element:--"<<endl;
cin>>search; ;
}
void Bisearch::sort()
{
for(i=0;i<n;i++)
{
  for(j=0;j<n-i;j++)
  {
    if(a[j]>a[j+1])
     {
      temp=a[j];
      a[j]=a[j+1];
      a[j+1]=temp;
     }
   }
 }
}
int Bisearch::searching()
{
int low=0,high=n-1,mid;
while(high>=low)
 {
  mid=(high+low)/2;
  if(a[mid]==search)
   return(mid+1);
  else
  {
   if(a[mid]<search)
      low=mid+1;
   else
      high=mid-1;
  }
 }
 return(-1);
}

void Bisearch::display()
 {
   cout<<"Elements after sorting"<<endl;
   for(i=0;i<n;i++)
   cout<<"\t"<<a[i];
   int result;
   result=searching();
   if(result==-1)
   cout<<"Elements not found"<<endl;
   else
   cout<<endl<<"Element is found at" <<setw(5) << result<<"position"<<endl;
 }
 
void main()
 {
   Bisearch bi;
   clrscr();
   bi.getdata();
   bi.searching();
   bi.sort();
   bi.display();
   getch();
 }

Binary search Program with Address -- Array -- Data -- Location
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class binarysearch
{
 public:
int a[33],i,j,start,end,mid,n,item,temp,flag;
 public:
void getdata();
void search();
};
void binarysearch::getdata()
{
 cout<<"Enter the size"<<endl;
 cin>>n;
 cout<<"Enter unsorted Array elements"<<endl;
 for(i=0;i<n;i++)
 {
  cout<<"Element a["<<i<<"] :"<<ends;
  cin>>a[i];
 }
 for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-1-i;j++)
      {
if(a[j]>a[j+1])
{
 temp=a[j];
 a[j]=a[j+1];
 a[j+1]=temp;
}
      }
    }
    cout<<"\n Sorted Array elements :";
    for(i=0;i<n;i++)
     {
      cout<<a[i]<<" ";
     }
    for(i=0;i<n;i++)
    {
     cout<<"\nAddress:"<< &a[i] <<"\tArray:"<<"a["<<i<<"]"<<"\tData:"<<a[i]<<"\tLocation:"<<i+1;
     cout<<"\n";
    }
}

void binarysearch::search()
{
 flag=0;
 cout<<"Enter the element to be searched"<<endl;
 cin>>item;
 start=0;end=n-1;
   while(start <= end)
      {
mid=(start+end)/2;
if(item < a[mid])
  end=mid - 1;
else if(item > a[mid])
  start=mid + 1;
else if(item == a[mid])
  {
   cout<<"\n Element"<<setw(5)<<item<<"is found at location"<<setw(5)<<mid+1;
   flag=1;
   break;
 }
       }
   if(flag==0)
cout<<"Element "<<setw(5)<<item<<"not found in the array";
}

void main()
{
binarysearch bs;
clrscr();
bs.getdata();
bs.search();
getch();
}

Output:

Binary search animation video link:
https://www.youtube.com/watch?v=sr_bR1WwcLY
 

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...