Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Search Code in C++ #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Binary_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;

// Show function is used to print array
void show(int a[], int arraysize)
{
for (int i = 0; i < arraysize; ++i)
cout << a[i] << " ";
cout<<endl;
}

int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2; //(l+r)/2 can be written but it may cause range problem

if (arr[mid] == x) // If the element is present at the middle
return mid;

if (arr[mid] > x) // If element is smaller than mid, then
return binarySearch(arr, l, mid-1, x); // it can only be present in left subarray

// Else the element can only be present in right subarray
return binarySearch(arr, mid+1, r, x);
}
// We reach here when element is not present in array
return -1;
}
int main()
{
int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int asize = sizeof(a) / sizeof(a[0]);
cout << "\n The array is : ";
show(a, asize);

sort(a, a + asize);//Binary sort is applied only on the sorted array
int element = 6;//element we want to search
if(binarySearch(a,0,10,element))
cout<<"You search: "<<element<<", it is present";
else
cout<<"You search: "<<element<<", it is not present";

return 0;
}