fix execution

This commit is contained in:
Robin Dietzel 2023-12-05 22:36:06 +01:00
parent 0c31a3c24e
commit a47714d8b6

View File

@ -3,6 +3,7 @@
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <functional> #include <functional>
#include <assert.h>
template<typename T> template<typename T>
class QuickSorterMT { class QuickSorterMT {
@ -49,8 +50,7 @@ private:
return; return;
} }
} }
auto pivot = *std::next(data.begin(), std::distance(data.begin(), data.end()) / 2);
auto pivot = data[std::distance(data.begin(), data.end()) / 2];
auto m1 = std::partition(data.begin(), data.end(), [pivot, this](const auto &em) { auto m1 = std::partition(data.begin(), data.end(), [pivot, this](const auto &em) {
return this->cmp(em, pivot); return this->cmp(em, pivot);
@ -60,8 +60,10 @@ private:
return !(this->cmp(em, pivot)); return !(this->cmp(em, pivot));
}); });
assert((m1 - data.begin()) <= data.size());
assert((m2 - data.begin()) <= data.size());
auto left = data.subspan(0, m1 - data.begin()); auto left = data.subspan(0, m1 - data.begin());
auto right = data.subspan(m2 - data.begin(), data.size()); auto right = data.subspan(m2 - data.begin());
if (depth < mdepth) { if (depth < mdepth) {
std::thread left_thread([&]() { qsort(left, depth + 1, mdepth); }); std::thread left_thread([&]() { qsort(left, depth + 1, mdepth); });