-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
35 lines (27 loc) · 793 Bytes
/
binary_search.cpp
File metadata and controls
35 lines (27 loc) · 793 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
#include <iostream>
#include <vector>
// returns the index in v of the element
int binary_search(std::vector<int> v, int start, int end, int n) {
int middle = end - start / 2;
std::cout << "Middle is " << middle << "\n";
if (n == v[middle]) {
return middle;
} else if (n > v[middle]) {
// go left
return binary_search(v, middle + 1, end, n);
} else {
// go right
return binary_search(v, start, middle - 1, n);
}
}
int main() {
std::vector<int> test;
test.push_back(0);
test.push_back(1);
test.push_back(2);
test.push_back(3);
test.push_back(5);
int index = binary_search(test, 0, test.size() - 1, 3);
std::cout << "The index of 3 is " << index << "\n";
return 0;
}