about-text

SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming languages. SoftOSet is the hub of the informative and research based articles.

Binary Search Program in C Plus Plus




In this article we will develop the C++ program of binary search.\

Searching and sorting are two efficient and most widely used concepts in data structure as well as in the modern technologies and internet world.

The program of Binary search will give the students some idea about how the internet works and how the search engines search the data from their database.


How this Program Works

All we have done is that we have simply transform the algorithm of Binary search in C++ programming language.

The algorithm of binary search is as under.

Conditions for Binary Search

Binary search algorithm works very fast when the data is sorted in increasing order. If this condition doesn’t fulfil then use any other search algorithm.


Algorithm of Binary Search


  1. BINARY (DATA, LB, UB, ITEM, LOC)
  2. Here DATA is a sorted array with lower bound LB and upper bound B, and ITEM is a given item of information. The variables BEG, END and MID denote respectively, the beginning, end and middle locations of a segment of elements of DATA. This algorithm finds the location LOC of ITEM in DATA or sets LOC=NULL.

  3. 1.      [Initialize segment variables.]                                                                                                                                                                         Set BEG:=LB, END:=UB and MID =INT((BEG+END)/2).
  4. 2.      Repeat Steps 3 and 4 while BEG<=END and DATA[MID]!=ITEM.
  5. 3.      If ITEM<DATA[MID], then:                               Set END:=MID-1.                                                                                                                                    Else:                                       Set BEG:=MID+1.                                                                                                                                           [End of If structure.]
  6. 4.      Set MID:=INT((BEG+END)/2).                                                                                                                          [End of Step 2 loop.]
  7. 5.      If DATA[MID]=ITEM, then:                                                                                                                            Set LOC:=MID.                                                                                                                           Else:                                                                                           
  8.        Set LOC:=NULL.                                                                                                            [End of If structure.]
  9. 6.      Exit.                                 

                               
How this Program Behave


This program will ask the user to enter a number. User will enter the number program will look in its database for this number.


If number is in the database program will show the location of that number.
And if not present in database, program will inform the user about its non-existence.


Program of Binary Search

//softoset.blogspot.com
//for sorted data
#include<iostream>
using namespace std;
int main()
{
            int Data[10]={1,2,3,4,5,6,7,8,9,10},item;
            cout<<"Enter the Element to Search "<<endl;
            cin>>item;
            int lb=1;
            int ub=10;
            int beg,end,mid;
            beg=lb;
            end=ub;
            mid=(beg+end)/2;
            for(int i=beg;(i<=end)&&(Data[mid]!=item);i++)
            {
                       
                        if(Data[mid]>item)
                        {
                                    end=mid-1;
                        }
                        else
                        {
                                    beg=mid+1;
                        }
                        mid=(beg+end)/2;
            }
            if(Data[mid]==item)
            cout<<"Element  "<<Data[mid]<<"   is Found  at Position   :"<<mid+1<<endl;//Indexing always starts with 0 in array
            else
            cout<<"Element Not Found "<<endl;
            return 0;
}

 Output of Program Binary Search


And here is the result of this program on execution Its response was like this.



 


You can clearly see that when user enter the 5 which was in the database program outputs that it is present and also show its location.




Now here user had entered the 15 which was not present so program shows that the number is not present.


Thank you for learning from the SoftOSet.
Reset Your Technology.



Post a Comment

Feel Free to Comment Us

Previous Post Next Post

Recents