You are on page 1of 12

 

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.

Home  C plus plus programs

Binary Search Program in C Plus Plus


 Ehsan Malik  June 06, 2020  0 Comments

  Facebook  Twitter   

Binary Search Program in C Plus Plus

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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
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.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
4. 1. [Initialize segment variables.]                                                                                                                                                                        
Set BEG:=LB, END:=UB and MID =INT((BEG+END)/2).
5. 2. Repeat Steps 3 and 4 while BEG<=END and DATA[MID]!=ITEM.
6. 3. If ITEM<DATA[MID], then:                               Set END:=MID-1.                
                                                                                                                   Else:                                       Set BEG:=MID+1.         
                                                                                                                                 [End of If structure.]
7. 4. Set MID:=INT((BEG+END)/2).                                                                                                                          [End of Step 2 loop.]
8. 5. If DATA[MID]=ITEM, then:                                                                                                                            Set
LOC:=MID.                                                                                                                           Else:   
                                                                                       
9.        Set LOC:=NULL.                                                                                                            [End of If structure.]
10. 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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
//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;
            }

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
            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.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 

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.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
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.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Tags C plus plus programs Data Structure

  Facebook  Twitter   

You might like Show more

Bubble Sort Program in C Plus Plus Binary Search Program in C Plus Plus Conversion of Infix to Postfix Expression
 June 06, 2020  June 06, 2020 Program in C Plus Plus
 June 06, 2020

Post a Comment

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Feel Free to Comment Us

Enter your comment...

Comment as: Google Accoun

Publish Preview

 Previous Post Next Post 

Follow Us

 1.5k  3.1k  2.7k

 500  1.8k  4.2k

  

  

Most Popular

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Currency Converter Program in C++
 May 04, 2020

Program in C++ to Calculate Fractions


 April 23, 2020

C++ Program to find Number of Days in Month


 April 29, 2020

Follow by Email
Get all latest content delivered straight to your inbox.

Email Address

Subscribe

Comments

razzaq
I have a similar interest this is my page read ...

sobia

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
I encourage you to read this text it is fun de...

Categories

Articles (5)

Books (2)

C Plus Plus Programs (36)

C Programs (6)

C Tutorial (2)

Data Structure (4)

Java (2)

Python (1)

Site Views

6,572

Main Tags

Articles Books C plus plus programs C Programs C Tutorial Data Structure Java Python

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Recents in Bitcoin

Error: No Results Found

SOFTOSET FOLLOW US

Reset Your Technology. We are passionate to deliver the best


    
and qualified content to our visitors.

Home Disclaimer Terms and Conditions FAQs


Copyright (c) 2020 SoftOSet All Right Reseved

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

You might also like