-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
108 lines (82 loc) · 2.57 KB
/
main.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
const int MAX_THREADS = 4; // Maximum number of threads
struct ThreadArgs {
std::vector<int>* arr;
int start;
int end;
};
void quicksort(std::vector<int>& arr, int low, int high) {
if (low >= high) {
return;
}
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
int partitionIndex = i + 1;
quicksort(arr, low, partitionIndex - 1);
quicksort(arr, partitionIndex + 1, high);
}
void hyperQuickSort(std::vector<int>& arr, int low, int high, int numThreads) {
std::stack<ThreadArgs> stack;
stack.push({ &arr, low, high });
while (!stack.empty()) {
ThreadArgs current = stack.top();
stack.pop();
int start = current.start;
int end = current.end;
if (start >= end) {
continue;
}
if (numThreads <= 1 || end - start < 2) {
quicksort(*current.arr, start, end);
continue;
}
int pivot = (*current.arr)[end];
int i = start - 1;
for (int j = start; j <= end - 1; j++) {
if ((*current.arr)[j] < pivot) {
i++;
std::swap((*current.arr)[i], (*current.arr)[j]);
}
}
std::swap((*current.arr)[i + 1], (*current.arr)[end]);
int partitionIndex = i + 1;
int threadsPerPartition = std::min(numThreads / 2, MAX_THREADS);
int threadsLeft = numThreads - threadsPerPartition;
stack.push({ current.arr, partitionIndex + 1, end });
if (threadsLeft > 0) {
stack.push({ current.arr, start, partitionIndex - 1 });
numThreads = threadsPerPartition;
} else {
quicksort(*current.arr, start, partitionIndex - 1);
}
}
}
int main() {
int numThreads, numElements;
std::cout << "Enter the number of threads: ";
std::cin >> numThreads;
std::cout << "Enter the number of elements in the array: ";
std::cin >> numElements;
std::cout << "Enter the elements of the array:" << std::endl;
std::vector<int> array(numElements);
for (int i = 0; i < numElements; i++) {
std::cin >> array[i];
}
hyperQuickSort(array, 0, numElements - 1, numThreads);
std::cout << "Sorted Array:" << std::endl;
for (int num : array) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}