**Problem:-**C++ Program Binary Search or C++ Program to Implement a Binary Search Algorithm for a Specific or Binary Search Program in C, C++ or Binary Search in C++ or Program that performs binary search or Binary Search Algorithm or C++ Program Code to perform binary search on an array of elements or Binary Search Array or Simple Binary search code example in cpp or Binary Search using C/C++ or Binary Search in C++ with Examples or C++ Programming: Binary Search Algorithm or How to Program Binary Search in C++ or Array C++ Binary Search or C program for binary search or C++ program to perform Insert, Delete, Search an element into a or Simple Binary Searching Program using functions in C++.

**Logic:-**Binary search logic is simple and very useful. Let's take an example of an array and try to perform a Binary search operation on it. First, we have to know that Binary search applies is only sorted data, so if data is not sorted then you can not apply Binary search. Now come to point so basically we divide the array data in two parts and compare the elements first iteration, and compare the element of the mid element of an array if the element is less than the array element then again divide the array from staring to midpoint into 2 part and again matches the element if again array element is greater than the element than again divide the array into two parts. If the array element is less the element than again divide the array into two parts starting to mid to end and repeat the step until to the last divide the array. If you are not getting than don;t worry Check below for step by step Explanation.

**Check This:-**C Program For Binary Search

**Explanation:-**We are taking an example of an array with the Non-Sorted element and performing a Binary search An Array is 9, 8, 3, 5, 4, 7, 1, 2, 6. Now follow the steps below.

**Step 1:-**First we have to sort the array or we can perform in already sorted array element but in this problem, we are sorting the array first. After the sorting, an array element is 1, 2, 3, 4, 5, 6, 7, 8, 9. total 9 elements. Now we have to Find the Element 7 in an array.

**Step 2:-**Now first we compare divide the array into two(Two Exactly). After the dividing, an array we got the midpoint Now compare the element 7 into the array element 5(MidPoint) we can clearly see that element is greater than the array element now we have to again divide the array into two part array is Now 6, 7, 8, 9.

**Step 3:-**Now divide the array and compare the element again after dividing an array we got a mid element of an array, Now compare an array mid to 7 we got the element in an array.

**Step 4:-**Now print the message that an Element is found in an Array. If Not found than Print Not Found the element in an Array.

**Note:-**Always perform the Binary search in the sorted element of an array, here I First sorted than performing. Sorting is compulsory for performing Binary search. If you are not entering the sorted array than you are not followed the Binary search properties.

**Key Note:-**Here is some feature of a Binary search.

1. Binary Search always performe in

**Sorted**Data.

2. Binary Search

**Worst-case**performance======>

**O(log n)**

3. Binary Search

**Best-case**performance=======>

**O(1)**

4. Binary Search

**Average**performance =======>

**O(log n)**

5. Binary Search

**Worst-case space**complexity==>

**O(1)**

**Check:-**Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

**Solution:-**

#include<iostream>

#include<stdlib.h>

using namespace std;

int main()

{

//C++ program for binary search

int *a,i,itm,s,j,flag=0,low,up,mid,temp;

cout<<"==================================";

cout<<"\nC++ Program For Binary Search\n";

cout<<"==================================\n";

cout<<"\nEnter The Size Of An Array :";

cin>>s;

a=(int*)malloc(s*sizeof(int));

cout<<"\nEnter An Elements Of An Array"<<endl;

for(i=0;i<s;i++)

{

cin>>a[i];

}

for(i=1;i<s;++i)

{

for(j=0;j<(s-1);++j)

{

if(a[j]>a[j+1])

{

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

}

cout<<"\nEnter The Number You Want To Search In An Array :";

cin>>itm;

low=0;

up=s-1;

mid=(low+up)/2;

if(itm<=a[mid])

{

for(i=0;i<=mid;i++)

{

if(a[i]==itm)

flag=1;

}

}

if(itm>a[mid])

{

for(i=mid;i<s;i++)

{

if(a[i]==itm)

flag=1;

}

}

if(flag==1)

{

cout<<"\nNumber Found In an Array \n";

}

else

{

cout<<"\nNumber Not Found In an Array \n";

}

}

**Output:-**

**You May Like This:-**

1. Hacker Rank solution for Attribute Parser

2. Hacker Rank solution for Vector-Sort

3. Hacker Rank solution for Vector-Erase

4. Java Program For Find The Gross Salary Of An Employee

5. Hacker Rank solution for Sets-STL

6. Hacker Rank solution for Maps-STL

7. Hacker Rank solution for Print Pretty

8. C++ Program For School Management System ( SMS Project ) With Source Code

9. Hacker Rank solution for Virtual Functions

10. C Program For Find A Grade Of Given Marks Using Switch Case

## No comments:

## Post a Comment