Skip to content

Instantly share code, notes, and snippets.

@dtaskoff
Created March 25, 2014 11:29
Show Gist options
  • Save dtaskoff/9759910 to your computer and use it in GitHub Desktop.
Save dtaskoff/9759910 to your computer and use it in GitHub Desktop.
// quicksort implementation
#include <vector>
int partition(std::vector<int>& v, int start, int end) {
int pivot = v[end];
int current = start;
for (int i = start; i < end; ++i) {
if (v[i] < pivot) {
std::swap(v[current], v[i]);
++current;
}
}
std::swap(v[current], v[end]);
return current;
}
void quicksort(std::vector<int>& v, int start, int end) {
while (start < end) {
int pivot = partition(v, start, end);
if (end - pivot > pivot - start) {
quicksort(v, start, pivot - 1);
start = pivot + 1;
} else {
quicksort(v, pivot + 1, end);
end = pivot - 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment