Binary Search Program in C Plus Plus
Binary Search Program in C Plus Plus
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.
Facebook Twitter
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.
All we have done is that we have simply transform the algorithm of Binary search in C++ programming language.
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.
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.
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;
}
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.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Tags C plus plus programs Data Structure
Facebook Twitter
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
Publish Preview
Follow Us
Most Popular
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Currency Converter Program in C++
May 04, 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 Programs (6)
C Tutorial (2)
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
SOFTOSET FOLLOW US
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD