-
Notifications
You must be signed in to change notification settings - Fork 3
/
nthelement.cpp
162 lines (142 loc) · 4.31 KB
/
nthelement.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <algorithm>
#include <random>
#include <chrono>
#include <cassert>
int comparisons;
int swaps;
std::mt19937 gen32;
struct Logger {
Logger(int v) : value(v) {}
int value;
};
bool operator <(const Logger &rhs, const Logger &lhs)
{
++comparisons;
return rhs.value < lhs.value;
}
bool operator>=(const Logger &rhs, const Logger &lhs)
{
++comparisons;
return rhs.value >= lhs.value;
}
bool operator<=(const Logger &rhs, const Logger &lhs)
{
++comparisons;
return rhs.value <= lhs.value;
}
void swap(Logger &lhs, Logger &rhs) noexcept {
++swaps;
std::swap(lhs.value, rhs.value);
}
typedef std::tuple<int,int, long long> Results;
Results partial_sort(const std::vector<Logger> &vals)
{
std::vector<Logger> work(vals);
comparisons = 0;
swaps = 0;
auto it = work.begin() + 1;
std::partial_sort(work.begin(), it + 1, work.end());
Results ret = {comparisons, swaps, 0};
assert(std::is_sorted(work.begin(), it + 1));
assert(std::all_of(work.begin (), it, [it](const Logger &v) {return v <= *it;}));
assert(std::all_of(it + 1, work.end(), [it](const Logger &v) {return v >= *it;}));
return ret;
}
Results nth_element(const std::vector<Logger> &vals)
{
std::vector<Logger> work(vals);
comparisons = 0;
swaps = 0;
auto it = work.begin() + 1;
std::nth_element(work.begin(), it, work.end());
Results ret = {comparisons, swaps, 0};
assert(std::is_sorted(work.begin(), it + 1));
assert(std::all_of(work.begin (), it, [it](const Logger &v) {return v <= *it;}));
assert(std::all_of(it + 1, work.end(), [it](const Logger &v) {return v >= *it;}));
return ret;
}
Results firstTwo(const std::vector<Logger> &vals)
{
std::vector<Logger> work(vals);
comparisons = 0;
swaps = 0;
bool printResults = false; // = vals.size() == 10U;
if (printResults) {
for (const auto& l: work)
std::cout << l.value << ' ';
std::cout << std::endl;
}
auto smallest = work.begin();
auto nextSmaller = work.begin() + 1;
if (*nextSmaller < *smallest)
std::swap(smallest, nextSmaller); // swapping iterators
for (auto walker = work.begin() + 2; walker != work.end(); ++walker) {
if (printResults) {
std::cout << smallest->value << " "
<< nextSmaller->value << " "
<< walker->value << std::endl;
}
if (*walker < *nextSmaller) {
if (*walker < *smallest) {
nextSmaller = smallest;
smallest = walker; // we moved it away!
}
else {
nextSmaller = walker;
}
}
}
if (printResults) {
std::cout << smallest->value << " "
<< nextSmaller->value << std::endl;
std::cout << std::distance(work.begin(), smallest) << " "
<< std::distance(work.begin(), nextSmaller) << std::endl;
}
// We have to be careful here:
// What if the "next smallest" is the first element?
// What if the "smallest element" is in position #1
if (smallest != work.begin()) {
std::iter_swap(smallest, work.begin());
if (nextSmaller == work.begin())
nextSmaller = smallest;
}
if (nextSmaller != work.begin() + 1)
std::iter_swap(nextSmaller, work.begin() + 1);
Results ret = {comparisons, swaps, 0};
if (printResults) {
for (const auto& l: work)
std::cout << l.value << ' ';
std::cout << std::endl;
}
auto it = work.begin() + 1;
assert(std::is_sorted(work.begin(), it + 1));
assert(std::all_of(work.begin (), it, [it](const Logger &v) {return v <= *it;}));
assert(std::all_of(it + 1, work.end(), [it](const Logger &v) {return v >= *it;}));
return ret;
}
void TestASize (size_t size)
{
std::vector<Logger> v;
v.reserve(size);
for (int i = 0; i < size; ++i)
v.push_back(gen32());
const auto [psComp, psSwap, psTime] = partial_sort(v);
const auto [neComp, neSwap, neTime] = nth_element(v);
const auto [ftComp, ftSwap, ftTime] = firstTwo(v);
std::cout << "Size = " << size << std::endl;
std::cout << "partial_sort: " << psComp << " comparisons and " << psSwap << " swaps" << " in " << psTime << "ms" << std::endl;
std::cout << "nth_element: " << neComp << " comparisons and " << neSwap << " swaps" << " in " << neTime << "ms" << std::endl;
std::cout << "firstTwo: " << ftComp << " comparisons and " << ftSwap << " swaps" << " in " << ftTime << "ms" << std::endl;
}
int main()
{
TestASize(10U);
TestASize(100U);
TestASize(1000U);
TestASize(10000U);
TestASize(100000U);
TestASize(1000000U);
// TestASize(10000000U);
// TestASize(100000000U);
}