Created
September 1, 2017 11:00
-
-
Save rdbuf/32abab219580267632ec8768e29537c4 to your computer and use it in GitHub Desktop.
A Quicksort implementation
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
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using elem_t = uint64_t; | |
template<class Container> | |
void qsort(const typename Container::iterator begin, const typename Container::iterator end) { | |
if (end - begin <= 1) { | |
return; | |
} | |
const auto pivot = *prev(end); | |
auto pivot_it = begin; | |
for (auto it = begin; it != end - 1; ++it) { | |
if (*it < pivot) { | |
iter_swap(pivot_it++, it); | |
} | |
} | |
if (*prev(end) < *pivot_it) { | |
iter_swap(prev(end), pivot_it); | |
} | |
qsort<Container>(begin, pivot_it); | |
qsort<Container>(++pivot_it, end); | |
} | |
int main() { | |
std::vector<elem_t> a; | |
copy(std::istream_iterator<elem_t>(std::cin), std::istream_iterator<elem_t>(), back_inserter(a)); | |
qsort<std::vector<elem_t>>(a.begin(), a.end()); | |
copy(a.begin(), a.end(), std::ostream_iterator<elem_t>(std::cout, ", ")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment