-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
65 lines (62 loc) · 2.26 KB
/
Copy pathquickSort.cpp
File metadata and controls
65 lines (62 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <list>
#include <algorithm>
#include <future>
template <typename T>
std::list<T> sequential_quick_sort(std::list<T> input)
{
if(input.empty())
return input;
std::list<T> result;
/*从input链表中分离出第一个元素,赋给result的第一个元素
作为比较的基准*/
result.splice(result.begin(),input,input.begin());
T const &pivot = *result.begin();
/*partition操作*/
auto divide_point = std::partition(input.begin(),input.end(),[&](T const &t){return t < pivot;});
std::list<T> lower_part;
/*把input从begin到分割点divide_point分离给lower_part*/
lower_part.splice(lower_part.end(),input,input.begin(),divide_point);
/*递归的去做partition,分割*/
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);
/*递归创建线程是指数级增加,一旦判定为线程过多,就会变为同步执行
也就是调用get时,执行函数*/
std::future<std::list<T>> new_lower(std::async(¶llel_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> test;
test.emplace_back(9);
test.emplace_back(2);
test.emplace_back(4);
test.emplace_back(10);
test.emplace_back(2);
for(auto i:test)
std::cout<<i<<" ";
std::cout<<std::endl;
auto result = parallel_quick_sort(test);
for(auto i:result)
std::cout<<i<<" ";
std::cout<<std::endl;
}