-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
49 lines (43 loc) · 942 Bytes
/
binary_search.cpp
File metadata and controls
49 lines (43 loc) · 942 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
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cassert>
#include <vector>
using std::vector;
int binary_search(const vector<int> &a, int x, int left, int right) {
int mid = (left+right)/2;
if( right >= left ){
if(a[mid] == x) return mid;
else if(a[mid] < x)
binary_search(a, x, mid+1, right);
else
{
binary_search(a, x, left, mid-1);
}
}
else
return -1;
}
int linear_search(const vector<int> &a, int x) {
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] == x) return i;
}
return -1;
}
int main() {
int n;
std::cin >> n;
vector<int> a(n);
for (size_t i = 0; i < a.size(); i++) {
std::cin >> a[i];
}
int m;
std::cin >> m;
vector<int> b(m);
for (int i = 0; i < m; ++i) {
std::cin >> b[i];
}
int l=0,r=(int)a.size()-1;
for (int i = 0; i < m; ++i) {
//replace with the call to binary_search when implemented
std::cout << binary_search(a, b[i], l, r) << ' ';
}
}