#include <algorithm>
#include <string>
#include <vector>
template<typename I>
void MergeSort(I first, I last) {
if (last - first > 1) {
I mid = first + (last - first) / 2;
MergeSort(first, mid);
MergeSort(mid, last);
std::inplace_merge(first, mid, last);
}
}
int main() {
for (int method = 2; method < 3; ++method) {
std::vector<std::string> a(1000000);
unsigned first = 0;
unsigned last = a.size();
for (unsigned i = 0; i < 100; ++i) {
const std::string s = std::string()
+ (char)('a' + i / 25)
+ (char)('a' + i % 25);
for (unsigned j = 0; j < i + 2; ++j) {
a[first++] = s;
}
a[--last] = s;
}
for (unsigned pos = first; pos < last; ++pos) {
a[pos] = std::string() + "z"
+ (char)('a' + pos % 23);
}
switch (method) {
case 0:
std::sort(a.begin(), a.end());
break;
case 1:
std::partial_sort(a.begin(), a.end(), a.end());
break;
case 2:
std::stable_sort(a.begin(), a.end());
break;
case 3:
MergeSort(a.begin(), a.end());
break;
}
}
return 0;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KCnRlbXBsYXRlPHR5cGVuYW1lIEk+CnZvaWQgTWVyZ2VTb3J0KEkgZmlyc3QsIEkgbGFzdCkgewoJaWYgKGxhc3QgLSBmaXJzdCA+IDEpIHsKCQlJIG1pZCA9IGZpcnN0ICsgKGxhc3QgLSBmaXJzdCkgLyAyOwoJCU1lcmdlU29ydChmaXJzdCwgbWlkKTsKCQlNZXJnZVNvcnQobWlkLCBsYXN0KTsKCQlzdGQ6OmlucGxhY2VfbWVyZ2UoZmlyc3QsIG1pZCwgbGFzdCk7Cgl9Cn0KCmludCBtYWluKCkgewoJZm9yIChpbnQgbWV0aG9kID0gMjsgbWV0aG9kIDwgMzsgKyttZXRob2QpIHsKCQlzdGQ6OnZlY3RvcjxzdGQ6OnN0cmluZz4gYSgxMDAwMDAwKTsKCQl1bnNpZ25lZCBmaXJzdCA9IDA7CgkJdW5zaWduZWQgbGFzdCA9IGEuc2l6ZSgpOwoJCWZv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