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

Create binary_sort.cpp #86

Merged
merged 1 commit into from
Oct 24, 2021
Merged
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
53 changes: 53 additions & 0 deletions C++ Programs/binary_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
using namespace std;

int binarySearch(int[], int, int, int);

int main()
{
int num[10] = {10, 22, 37, 55, 92, 118};
int search_num, loc=-1;

cout<<"Enter the number that you want to search: ";
cin>>search_num;

loc = binarySearch(num, 0, 6, search_num);

if(loc != -1)
{
cout<<search_num<<" found in the array at the location: "<<loc;
}
else
{
cout<<"Element not found";
}
return 0;
}

int binarySearch(int a[], int first, int last, int search_num)
{
int middle;
if(last >= first)
{
middle = (first + last)/2;
//Checking if the element is present at middle loc
if(a[middle] == search_num)
{
return middle+1;
}

//Checking if the search element is present in greater half
else if(a[middle] < search_num)
{
return binarySearch(a,middle+1,last,search_num);
}

//Checking if the search element is present in lower half
else
{
return binarySearch(a,first,middle-1,search_num);
}

}
return -1;
}