Skip to content

Instantly share code, notes, and snippets.

@chapuni chapuni/sort.cc
Created Oct 30, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.