Last active
November 5, 2017 04:03
-
-
Save tacigar/ad894617515bcad26b63e58bf927ab6d to your computer and use it in GitHub Desktop.
STLなクイックソートなり.
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 <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