-
Notifications
You must be signed in to change notification settings - Fork 14
/
Approximate_sort.cpp
88 lines (81 loc) · 2 KB
/
Approximate_sort.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
#include <functional>
#include <iostream>
#include <queue>
#include <random>
#include <sstream>
#include <string>
#include <vector>
using std::cout;
using std::default_random_engine;
using std::endl;
using std::greater;
using std::istringstream;
using std::priority_queue;
using std::random_device;
using std::string;
using std::stringstream;
using std::uniform_int_distribution;
using std::vector;
// @include
void approximate_sort(istringstream* sin, int k) {
priority_queue<int, vector<int>, greater<int>> min_heap;
// Firstly push k elements into min_heap.
int x;
for (int i = 0; i < k && *sin >> x; ++i) {
min_heap.push(x);
}
// Extract the minimum one for every incoming element.
while (*sin >> x) {
min_heap.push(x);
cout << min_heap.top() << endl;
min_heap.pop();
}
// Extract the remaining elements in min_heap.
while (!min_heap.empty()) {
cout << min_heap.top() << endl;
min_heap.pop();
}
}
// @exclude
// It should print 1, 2, 3, 4, 5, 6, 7, ,8, 9.
void simple_test() {
vector<int> A = {2, 1, 5, 4, 3, 9, 8, 7, 6};
stringstream ss;
for (int a : A) {
ss << a << ' ';
}
istringstream sin(ss.str());
approximate_sort(&sin, 3);
}
int main(int argc, char* argv[]) {
simple_test();
default_random_engine gen((random_device())());
int n, k;
if (argc == 2) {
n = atoi(argv[1]);
uniform_int_distribution<int> dis(1, n);
k = dis(gen);
} else if (argc == 3) {
n = atoi(argv[1]);
k = atoi(argv[2]);
} else {
uniform_int_distribution<int> n_dis(1, 100000);
n = n_dis(gen);
uniform_int_distribution<int> k_dis(1, n);
k = k_dis(gen);
}
cout << "n = " << n << " k = " << k << endl;
vector<int> A;
uniform_int_distribution<int> dis(1, 999999);
for (int i = 0; i < n; ++i) {
A.push_back(dis(gen));
}
stringstream ss;
for (int a : A) {
ss << a << ' ';
}
istringstream sin(ss.str());
approximate_sort(&sin, n - 1);
return 0;
}