Qsort in C++ using vector
up vote
0
down vote
favorite
I tried to make a simple qsort() function in a functional way, it takes a vector and a bool pred()
Suggestions as to make it more functional, optimize it, and/or remove extra steps and intermediate variables are appreciated
template <typename T>
void operator+=(std::vector<T> &v1, const std::vector<T> &v2) {
v1.insert(v1.end(), v2.begin(), v2.end());
}
template <typename T, typename Func>
std::vector<T> qsort(const std::vector<T>& in, const Func& pred) {
std::vector<T> out;
std::vector<T> less;
std::vector<T> more;
if (in.size() < 2)
return in;
auto p = in[0];
for (auto i = 1; i < in.size(); i++) {
if (pred(p, in[i]))
less.push_back(in[i]);
else
more.push_back(in[i]);
}
out = qsort(less, pred);
out.push_back(p);
out += qsort(more, pred);
return out;
}
c++ algorithm vectors c++17 quick-sort
add a comment |
up vote
0
down vote
favorite
I tried to make a simple qsort() function in a functional way, it takes a vector and a bool pred()
Suggestions as to make it more functional, optimize it, and/or remove extra steps and intermediate variables are appreciated
template <typename T>
void operator+=(std::vector<T> &v1, const std::vector<T> &v2) {
v1.insert(v1.end(), v2.begin(), v2.end());
}
template <typename T, typename Func>
std::vector<T> qsort(const std::vector<T>& in, const Func& pred) {
std::vector<T> out;
std::vector<T> less;
std::vector<T> more;
if (in.size() < 2)
return in;
auto p = in[0];
for (auto i = 1; i < in.size(); i++) {
if (pred(p, in[i]))
less.push_back(in[i]);
else
more.push_back(in[i]);
}
out = qsort(less, pred);
out.push_back(p);
out += qsort(more, pred);
return out;
}
c++ algorithm vectors c++17 quick-sort
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I tried to make a simple qsort() function in a functional way, it takes a vector and a bool pred()
Suggestions as to make it more functional, optimize it, and/or remove extra steps and intermediate variables are appreciated
template <typename T>
void operator+=(std::vector<T> &v1, const std::vector<T> &v2) {
v1.insert(v1.end(), v2.begin(), v2.end());
}
template <typename T, typename Func>
std::vector<T> qsort(const std::vector<T>& in, const Func& pred) {
std::vector<T> out;
std::vector<T> less;
std::vector<T> more;
if (in.size() < 2)
return in;
auto p = in[0];
for (auto i = 1; i < in.size(); i++) {
if (pred(p, in[i]))
less.push_back(in[i]);
else
more.push_back(in[i]);
}
out = qsort(less, pred);
out.push_back(p);
out += qsort(more, pred);
return out;
}
c++ algorithm vectors c++17 quick-sort
I tried to make a simple qsort() function in a functional way, it takes a vector and a bool pred()
Suggestions as to make it more functional, optimize it, and/or remove extra steps and intermediate variables are appreciated
template <typename T>
void operator+=(std::vector<T> &v1, const std::vector<T> &v2) {
v1.insert(v1.end(), v2.begin(), v2.end());
}
template <typename T, typename Func>
std::vector<T> qsort(const std::vector<T>& in, const Func& pred) {
std::vector<T> out;
std::vector<T> less;
std::vector<T> more;
if (in.size() < 2)
return in;
auto p = in[0];
for (auto i = 1; i < in.size(); i++) {
if (pred(p, in[i]))
less.push_back(in[i]);
else
more.push_back(in[i]);
}
out = qsort(less, pred);
out.push_back(p);
out += qsort(more, pred);
return out;
}
c++ algorithm vectors c++17 quick-sort
c++ algorithm vectors c++17 quick-sort
asked 11 hours ago
Meme myself and a very creepy
564
564
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207943%2fqsort-in-c-using-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown