Created
October 24, 2015 22:27
-
-
Save goldsborough/c0eddbd61817d792b206 to your computer and use it in GitHub Desktop.
Hopefully finally edge-case-secure partitioning algorithm.
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<typename Iterator> | |
Iterator partition(Iterator begin, Iterator end) | |
{ | |
auto pivot = begin++; | |
if (pivot == end || begin == end) return pivot; | |
for (--end; begin != end; ++begin) | |
{ | |
while (begin != end && *begin < *pivot) ++begin; | |
while (begin != end && *end > *pivot) --end; | |
if (begin == end) break; | |
std::swap(*begin, *end); | |
} | |
if (*begin > *pivot) --begin; | |
std::swap(*begin, *pivot); | |
return begin; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment