Skip to content

Instantly share code, notes, and snippets.

@socantre
Created September 4, 2014 16:08
Show Gist options
  • Select an option

  • Save socantre/57185beb0935b763ca31 to your computer and use it in GitHub Desktop.

Select an option

Save socantre/57185beb0935b763ca31 to your computer and use it in GitHub Desktop.
construct worst case for arbitrary? sort algorithm
#include <vector>
#include <algorithm>
void make_killer(int size, std::vector<int>& v) {
int candidate = 0;
int num_solid = 0;
int gas = size - 1;
std::vector<int> tmp(size);
v.resize(size);
for (int i = 0; i < size; ++i) {
tmp[i] = i;
v[i] = gas;
}
std::sort(tmp.begin(), tmp.end(), [&](int x, int y) {
if (v[x] == gas && v[y] == gas) {
if (x == candidate) v[x] = num_solid++;
else v[y] = num_solid++;
}
if (v[x] == gas) candidate = x;
else if (v[y] == gas) candidate = y;
return v[x] < v[y];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment