forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.cpp
59 lines (52 loc) · 1.03 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
#include <iostream>
#include <cassert>
#include <vector>
#include<bits/stdc++.h>
using namespace std;
long int binary_search_1(vector<long int> a, int x) {
long int left = 0, right = (int)a.size();
int mid ;
int i = 0;
while(left < right){
mid = (left + right) / 2;
if(a[mid] == x){
return mid;
}
else if(a[mid] < x){
left= mid + 1;
}
else {
right = mid - 1;
}
}
return -1;
}
//write your code here
int linear_search(vector<long int> a, int x) {
for (int i = 0; i < a.size(); ++i) {
if (a[i] == x) return i;
}
return -1;
}
//5 1 5 8 12 13
//5 8 1 23 1 11
int main() {
int n;
cin >> n;
vector<long int> a(n);
for (int i = 0; i < a.size(); i++) {
cin >> a[i];
}
int m;
cin >> m;
vector<long int> b(m);
for (int i = 0; i < m; ++i) {
cin >> b[i];
}
for (int i = 0; i < m; ++i) {
//replace with the call to binary_search when implemented
// cout << linear_search(a, b[i]) << ' ';
//cout<<endl;
cout << binary_search_1(a, b[i]) << ' ';
}
}