Last active
May 27, 2016 22:58
-
-
Save DieHertz/11216051 to your computer and use it in GitHub Desktop.
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 <vector> | |
#include <functional> | |
#include <algorithm> | |
#include <iterator> | |
#include <iostream> | |
#include <utility> | |
#include <type_traits> | |
template<typename T> auto read_vector() { | |
return std::vector<T>{std::istream_iterator<T>{std::cin}, std::istream_iterator<T>{}}; | |
} | |
template<typename T> auto operator+(std::vector<T> lhs, const std::vector<T>& rhs) { | |
std::copy(std::begin(rhs), std::end(rhs), std::back_inserter(lhs)); | |
return lhs; | |
} | |
template<typename T> auto operator+(std::vector<T> lhs, const T& rhs) { | |
lhs.push_back(rhs); | |
return lhs; | |
} | |
template<typename T, typename F> auto filter(const std::vector<T>& v, F&& f) { | |
auto ret = std::vector<T>{}; | |
std::copy_if(std::begin(v), std::end(v), std::back_inserter(ret), std::forward<F>(f)); | |
return ret; | |
} | |
template<typename T> std::vector<T> quicksort(const std::vector<T>& v) { | |
return v.size() < 2 ? v : | |
quicksort(filter(v, std::bind(std::greater<T>{}, v.front(), std::placeholders::_1))) + | |
v.front() + | |
quicksort(filter(v, std::bind(std::less<T>{}, v.front(), std::placeholders::_1))); | |
} | |
int main() { | |
const auto v = quicksort(read_vector<int>()); | |
std::for_each(std::begin(v), std::end(v), [] (auto item) { std::cout << item << ' '; }); | |
std::cout << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment