Created
March 7, 2021 17:19
-
-
Save commander-trashdin/364699fb9d52e8258b3aeebc04dc63cf to your computer and use it in GitHub Desktop.
showing how to do this
This file contains hidden or 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
| 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