Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(ALGORITHMS_ON_STRINGS_LIB_SRC
)

set(SORTING_ALGORITHMS_LIB_SRC
sorting_algorithms/quick_sort/parallel_and_sequential_quick_sort.cpp
sorting_algorithms/quick_sort/qsort_test.cpp
sorting_algorithms/quick_sort/template_qsort.h
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <algorithm>
#include <cstdio>
#include <future>
#include <iostream>
#include <list>
#include <memory>
#include <thread>

std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list) {
for(auto& i : list) {
ostr << " " << i;
}
return ostr;
}

template<typename T>
std::list<T> sequential_quick_sort(std::list<T> input) {
if(input.empty()) {
return input;
}
std::list<T> result;
result.splice(result.begin(), input, input.begin());
T const& pivot = *result.begin();
auto divide_point = std::partition(
input.begin(), input.end(), [&](T const& t) { return t < pivot; });
std::list<T> lower_part;
lower_part.splice(lower_part.end(), input, input.begin(), divide_point);
auto new_lower(sequential_quick_sort(std::move(lower_part)));
auto new_higher(sequential_quick_sort(std::move(input)));
result.splice(result.end(), new_higher);
result.splice(result.begin(), new_lower);
return result;
}

template<typename T>
std::list<T> parallel_quick_sort(std::list<T> input) {
if(input.empty()) {
return input;
}
std::list<T> result;
result.splice(result.begin(), input, input.begin());
T const& pivot = *result.begin();
auto divide_point = std::partition(
input.begin(), input.end(), [&](T const& t) { return t < pivot; });
std::list<T> lower_part;
lower_part.splice(lower_part.end(), input, input.begin(), divide_point);
std::future<std::list<T> > new_lower(
std::async(&parallel_quick_sort<T>, std::move(lower_part)));
auto new_higher(parallel_quick_sort(std::move(input)));
result.splice(result.end(), new_higher);
result.splice(result.begin(), new_lower.get());
return result;
}

int main() {
std::list<int> list1 = {4, 2, 1, 5, 8, 9, 7};
std::list<int> sorted1 = sequential_quick_sort(list1);
std::list<int> sorted2 = parallel_quick_sort(list1);
std::cout << "sorted1: " << sorted1 << std::endl;
std::cout << "sorted2: " << sorted2 << std::endl;
return 0;
}