-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.cpp
63 lines (59 loc) · 1.13 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
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cassert>
#include <vector>
using std::vector;
int binary_search(vector<int> &a, int x)
{
long long left = 0;
long long right = (int)a.size() - 1;
while (left <= right)
{
long long mid = left + (right - left) / 2;
if (x == a[mid])
{
return mid;
}
if (a[mid] < x)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
//write your code here
// int mid = left + right / 2;
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];
}
for (int i = 0; i < m; ++i)
{
//replace with the call to binary_search when implemented
std::cout << binary_search(a, b[i]) << ' ';
}
}