Skip to content

Instantly share code, notes, and snippets.

@commander-trashdin
Created March 7, 2021 17:19
Show Gist options
  • Select an option

  • Save commander-trashdin/364699fb9d52e8258b3aeebc04dc63cf to your computer and use it in GitHub Desktop.

Select an option

Save commander-trashdin/364699fb9d52e8258b3aeebc04dc63cf to your computer and use it in GitHub Desktop.
showing how to do this
template <class Iterator, class RandomGenerator>
void QuickSort(Iterator begin, Iterator end, RandomGenerator& generator) {
if (begin == end) {
return;
}
std::uniform_int_distribution<> dis(0, end - begin - 1);
auto pivot = *(begin + dis(generator));
auto first = begin;
auto last = end - 1;
if (begin == last) {
return;
}
auto mid = begin;
while (mid <= last) {
if (*mid < pivot) {
std::iter_swap(first, mid);
++first;
++mid;
} else if (pivot < *mid) {
std::iter_swap(mid, last);
--last;
} else {
++mid;
}
}
++last;
QuickSort(begin, first, generator);
QuickSort(last, end, generator);
}
template <class Iterator>
void Sort(Iterator begin, Iterator end) {
std::mt19937 generator;
QuickSort(begin, end, generator);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment