-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch
More file actions
39 lines (39 loc) · 784 Bytes
/
binarySearch
File metadata and controls
39 lines (39 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Asset
#include<bits/stdc++.h>
using namespace std;
void binarySearch(int arr[], int element, int len)
{
int low = 0;
int high = len;
while (low <= high)
{
int mid = (high+low)/2;
int guess = arr[mid];
if (guess == element)
{
cout<<"Element found at "<<mid<<" th index";
return;
}
else if (guess < element)
low = mid + 1;
else if (guess > element)
high = mid - 1;
}
cout<<"Not found";
return;
}
int main()
{
int n,elem;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
sort(arr,arr+n);
for(int i=0;i<n;i++)
cout<<arr[i]<<' ';
cout<<endl;
cin>>elem;
binarySearch(arr, elem, n);
return 0;
}