Skip to content

Instantly share code, notes, and snippets.

@tacigar
Last active November 5, 2017 04:03
Show Gist options
  • Save tacigar/ad894617515bcad26b63e58bf927ab6d to your computer and use it in GitHub Desktop.
Save tacigar/ad894617515bcad26b63e58bf927ab6d to your computer and use it in GitHub Desktop.
STLなクイックソートなり.
#include <algorithm>
#include <vector>
#include <iostream>
#include <list>
template <class BidirectionalIterator>
auto quickSort(BidirectionalIterator first, BidirectionalIterator last) -> void
{
auto l = first, r = std::prev(last);
while (l != r) {
while (*l < *first) l++;
while (*r >= *first && l != r) r--;
if (l != r) {
std::swap(*r, *l);
}
}
if (l != first) quickSort(first, l);
if (std::next(l) != last) quickSort(std::next(l), last);
}
int main()
{
{
std::vector<int> v = {2, 3, 1, 10, 20, 3, 4, 58};
quickSort(std::begin(v), std::end(v));
for (auto value : v) {
std::cout << value << " ";
}
std::cout << std::endl;
}
{
std::list<int> l = {2, 3, 1, 10, 20, 3, 4, 58};
quickSort(std::begin(l), std::end(l));
for (auto value : l) {
std::cout << value << " ";
}
std::cout << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment