Skip to content
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
41 changes: 41 additions & 0 deletions Binary_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SATYAMEVA JAYATE
// To find an element in increasing seq. of values
#include <bits/stdc++.h>
using namespace std;

int arr[100];
int binary(int l,int r,int key)
{
while(l<=r)
{
int mid = (l+r)/2;
if(arr[mid] == key) // key is the element to find
{
return mid;
}
else if( arr[mid] > key)
{
r = mid-1;
}
else
{
l = mid+1;
}
}
return -1; // Not Found
}
int main()
{
int n,m,i;
cin >> n; // Size of the array
for(i=0;i<n;i++)
{
cin >> arr[i];
}
sort(arr,arr+n); // Hence the array is sorted
cout<<"Enter the element to be searched\n";
cin >> m;
int x = binary(0,n,m);
cout << x<<"\n";
return 0;
}