-
Notifications
You must be signed in to change notification settings - Fork 14
/
Binary_search_first_k.cpp
62 lines (58 loc) · 1.53 KB
/
Binary_search_first_k.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
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
using std::cout;
using std::default_random_engine;
using std::endl;
using std::random_device;
using std::uniform_int_distribution;
using std::vector;
// @include
int search_first(const vector<int>& A, int k) {
int l = 0, r = A.size() - 1, res = -1;
while (l <= r) {
int m = l + ((r - l) >> 1);
if (A[m] > k) {
r = m - 1;
} else if (A[m] == k) {
// Record the solution and keep searching the left part.
res = m, r = m - 1;
} else { // A[m] < k
l = m + 1;
}
}
return res;
}
// @exclude
int main(int argc, char* argv[]) {
default_random_engine gen((random_device())());
for (int times = 0; times < 1000; ++times) {
int n;
if (argc == 2) {
n = atoi(argv[1]);
} else {
uniform_int_distribution<int> dis(1, 100000);
n = dis(gen);
}
vector<int> A;
uniform_int_distribution<int> k_dis(0, n - 1);
int k = k_dis(gen);
for (int i = 0; i < n; ++i) {
uniform_int_distribution<int> dis(0, n - 1);
A.emplace_back(dis(gen));
}
sort(A.begin(), A.end());
int ans = search_first(A, k);
cout << "k = " << k << " locates at " << ans << endl;
if (ans != -1) {
cout << "A[k] = " << A[ans] << endl;
}
auto it = find(A.cbegin(), A.cend(), k);
assert((it == A.cend() && ans == -1) || (distance(A.cbegin(), it) == ans));
}
return 0;
}