Created
March 25, 2014 11:29
-
-
Save dtaskoff/9759910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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