Q:

C++ Program For Binary Search

0

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.

 

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)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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:

==================================

C++ Program For Binary Search

==================================

 

Enter The Size Of An Array :7

 

Enter An Elements Of An Array

40

30

20

50

60

10

70

 

Enter The Number You Want To Search In An Array :50

Number Found In an Array

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now