forked from siddharth25pandey/CPP-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
39 lines (31 loc) · 901 Bytes
/
binary_search.cpp
File metadata and controls
39 lines (31 loc) · 901 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
// Program to search for a given a number in a sorted vector using binary search
#include <iostream>
#include <vector>
using namespace std;
// Where n is the number you want to find and arr is the vector you search in
int bin_search(int n, vector<int>vec){
// L and R will be the boundaries of the search
int L = 0;
int R = vec.size() - 1 / 2;
while(L <= R){
// half will be the floor of L + R / 2
int half = (L + R) / 2;
if(vec[half] < n){
L = half + 1;
}
else if(vec[half] > n) {
R = half - 1;
}
else{
return half;
}
}
// the function returns -1 if the number is not present in the vector
return -1;
}
int main() {
vector<int> test_vec = {1, 4, 7, 10, 12, 15};
int test_number;
cin >> test_number;
cout << bin_search(test_number, test_vec);
}