Skip to content

Instantly share code, notes, and snippets.

@caotic123
Last active March 21, 2019 01:00
Show Gist options
  • Save caotic123/f13344570e4b155b65b00b9dfb1b930e to your computer and use it in GitHub Desktop.
Save caotic123/f13344570e4b155b65b00b9dfb1b930e to your computer and use it in GitHub Desktop.
C++11 - A modern fully recursive quick sort.
#include <iostream>
#include <algorithm>
#include <tuple>
#include <vector>
#include <functional>
template <typename T>
std::vector<T> qsort_(std::vector<T> vector,
std::function<bool(T i, T x)> c_m) {
std::function<std::tuple<int, int, std::vector<T>, T> (
std::tuple<int, int, std::vector<T>, T> vec_s)> __qsort =
[&](std::tuple<int, int, std::vector<T>, T> vec_s) {
std::function<std::tuple<int, int, std::vector<T>, T> (
std::vector<T> ___, T s_, std::tuple<bool, bool> p_, int i, int j,
int l, int r)> ordn_ = [&](std::vector<T> ___, T s_,
std::tuple<bool, bool> p_, int i, int j,
int l, int r) { //função que faz a iteração de procurar dois valores a serem trocados
std::function<std::tuple<int, int, std::vector<T>, T> (
std::vector<T>, int, int)> _swap = [&](std::vector<T> ___, int i,
int j) { // esta função troca os valores se i <= j
if (i <= j) {
std::swap(___[i], ___[j]);
}
return std::make_tuple((i <= j) ? i + 1 : i, (i <= j) ? j - 1 : j,
___, s_);
}; //retorna uma tupla onde (i+1, j-1, vetor, pivo)
return (std::get<0>(p_) && std::get<1>(p_))
? _swap(___, i, j)
: ((___[i] == s_ || (c_m(___[i], s_)) || (i < l)) &&
!std::get<0>(p_))
? ordn_(___, s_,
std::make_tuple(true, std::get<1>(p_)), i, j,
l, r)
: (((___[j] == s_ || !c_m(___[j], s_)) || (j < l)) &&
!std::get<1>(p_))
? ordn_(___, s_,
std::make_tuple(std::get<0>(p_), true),
i, j, l, r)
: ordn_(___, s_, p_,
(!std::get<0>(p_) ? i + 1 : i),
(!std::get<1>(p_) ? j - 1 : j), l, r);
}; // Se os valores forem passiveis de troca então retorna o vetor com a troca feito é i+1 e j-1
std::function<std::tuple<int, int, std::vector<T>, T> (
std::tuple<int, int, std::vector<T>, T> ____, int l,
int r)> quick_ = [&](std::tuple<int, int, std::vector<T>, T> ____,
int l, int r) { // esta função faz a troca de valores entre o numero central onde {n (x) m} x = central
return (std::get<0>(____) <= std::get<1>(____))
? quick_(ordn_(std::get<2>(____), std::get<3>(____),
std::make_tuple(false, false),
std::get<0>(____), std::get<1>(____), l, r),
l, r)
: ____;
}; // a função retorna apenas se i <= j (possivelmente pela a movimentação da variavel central
std::tuple<int, int, std::vector<T>, T> ____ = quick_(
std::make_tuple(
std::get<0>(vec_s), std::get<1>(vec_s), std::get<2>(vec_s),
std::get<2>(
vec_s)[((std::get<0>(vec_s) + std::get<1>(vec_s)) / 2)]),
std::get<0>(vec_s), std::get<1>(vec_s)); //faz uma iteração de troca
if (std::get<1>(____) > std::get<0>(vec_s)) { //compara o i com o left passado
std::get<2>(____) = std::get<2>(__qsort(std::make_tuple(
std::get<0>(vec_s), std::get<1>(____), std::get<2>(____),
std::get<2>(
____)[(std::get<0>(vec_s) + std::get<1>(____)) / 2]))); //resolve um sub-problema do sub-vector
}
if (std::get<1>(vec_s) > std::get<0>(____)) {
std::get<2>(____) = std::get<2>(__qsort(std::make_tuple(
std::get<0>(____), std::get<1>(vec_s), std::get<2>(____),
std::get<2>(
____)[(std::get<1>(vec_s) + std::get<0>(____)) / 2]))); //retorna a um sub vetor
}
return ____; //tudo certo FIM
};
return std::get<2>(__qsort(std::make_tuple(0, vector.size() - 1, vector, 0))); // pega o vetor da tupla e da o ultimo return
}
@caotic123
Copy link
Author

For use :qsort_<T>(vec_, [](int x, int y) { return x > y; });

@ryukinix
Copy link

WTF IS THIS

@caotic123
Copy link
Author

WTF IS THIS

This is almost readable c++ quick sort :#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment