-
Notifications
You must be signed in to change notification settings - Fork 368
/
binary_search.cpp
51 lines (45 loc) · 1.34 KB
/
binary_search.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
/*
<----------------------------------PROBLEM------------------------------------>
Given a sorted array arr[] of n elements, write a function to search a given
element x in arr[] and return the index of x in the array.
Variables used:
n = total number of elements in array.
v = vector (dynamic array initialized with the input size).
el = element to be searched in the array.
lt = left-most index of the search space.
rt = right-most index of the search space.
*/
int main()
{
int n;
cin >> n;
vector<int> v(n);
//Enter sorted array - binary search always works on sorted array
for(int i=0; i<n; i++){
cin >> v[i];
}
int el;
cin >> el;
int lt=0, rt=n-1;
while(lt<=rt){
// in order to prevent overflow of `int`, it has been written lt + (rt-lt)/2 instead of (lt+rt)/2;
int mid = lt + (rt-lt)/2;
// element is equal to mid, return the index
if(v[mid]==el){
cout << "Element found:)\n";
return 0;
}
// element is larger than mid, then shift search space to right.
else if(v[mid]<el){
lt=mid+1;
}
// element is smaller than mid, then shift search space to left.
else{
rt=mid-1;
}
}
cout << "Element not found:(\n";
return 0;
}