Skip to content

Instantly share code, notes, and snippets.

@chapuni
Created October 30, 2017 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chapuni/9c5659610f5cd3c0933708bd43f32cdb to your computer and use it in GitHub Desktop.
Save chapuni/9c5659610f5cd3c0933708bd43f32cdb to your computer and use it in GitHub Desktop.
Prove how sort was insufficient
#include <algorithm>
#include <iostream>
#include <vector>
static bool cmp(int a, int b) {
return ((a == 3 && b == 0)
|| (a == 4 && b == 2)
|| (a == 5 && b == 1));
}
template <typename V>
void print(const V& ary) {
for (auto a: ary) std::cout << " " << a;
}
int main() {
std::vector<int> ary = {0, 1, 2, 3, 4, 5};
do {
std::vector<int> sorted = ary;
std::stable_sort(sorted.begin(), sorted.end(), cmp);
for (int i = 0; i < 6 - 1; ++i)
for (int j = i + 1; j < 6; ++j)
if (cmp(sorted[j], sorted[i])) {
print(ary);
std::cout << " =>";
print(sorted);
std::cout << " (" << sorted[i] << "," << sorted[j] << ")" << std::endl;
goto quit;
}
#if 0
print(sorted);
std::cout << std::endl;
#endif
quit:;
} while (std::next_permutation(ary.begin(), ary.end()));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment