-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqSortMT.hpp
More file actions
58 lines (51 loc) · 1.54 KB
/
Copy pathqSortMT.hpp
File metadata and controls
58 lines (51 loc) · 1.54 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
#pragma once
#include <thread>
//C++11
//Declarations
/**
* Generic multithreaded Quick sort.
* @tparam T - Generic type argument.
* @param arr - Array of elements.
* @param size - Size of array.
* @param max_depth - 2^max_depth determines number of threads
* (For example if max_depth = 3, then there will be 8 threads working on sorting.).
*/
template<class T>
void quickSortMT(T* arr, int size, int max_depth = 2);
//Auxiliary function
template<class T>
void quickSortAuxMT(T* arr, int size, int maxDepth, int curDepth);
//Definitions
template<class T>
void quickSortAuxMT(T* arr, int size, int maxDepth, int curDepth) {
int p, left = 1, right = size - 1;
if (size < 2)
return;
//partition
std::swap(arr[0], arr[size / 2]);
p = arr[0];
while (left <= right) {
while (right >= left && arr[right] >= p)
right--;
while (left <= right && arr[left] < p)
left++;
if (left < right)
std::swap(arr[left++], arr[right--]);
}
std::swap(arr[0], arr[right]);
//end of partition
if (curDepth <= maxDepth) {
std::thread t(quickSortAuxMT<T>, arr, right, maxDepth, curDepth + 1);
std::thread t2(quickSortAuxMT<T>, arr + right + 1, size - right - 1, maxDepth, curDepth + 1);
//wait for threads to finish
t.join();
t2.join();
} else {
quickSortAuxMT(arr, right, maxDepth, curDepth + 1);
quickSortAuxMT(arr + right + 1, size - right - 1, maxDepth, curDepth + 1);
}
}
template<class T>
void quickSortMT(T* arr, int size, int max_depth) {
quickSortAuxMT(arr, size, max_depth, 1);
}