-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelQuicksort.cpp
More file actions
61 lines (49 loc) · 1.36 KB
/
parallelQuicksort.cpp
File metadata and controls
61 lines (49 loc) · 1.36 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
/* Andrew Rossman
* Experimenting with parallel programming using sorting algorithm
* 01/21/2023
*/
#include<iostream>
//#include<upcxx/upcxx.hpp>
void quicksort(int[], int, int);
int partition(int[], int, int);
void swap(int*, int*);
int main(int argc, char *argv[]) {
int testArray[] = {19, 57, 45, 8, 12, 6, 1, 52, 24, 38};
int testArrayLength = sizeof(testArray) / sizeof(testArray[0]);
std::cout << "Unsorted array : " ;
for(int number : testArray){
std::cout << number << ", ";
}
std::cout << "\n";
quicksort(testArray, 0, testArrayLength-1);
std::cout << "Sorted array : " ;
for(int number : testArray){
std::cout << number << ", ";
}
std::cout << "\n";
return -1;
}
void quicksort(int sortingArray[], int low, int high) {
if(low < high) {
int partitionIndex = partition(sortingArray, low, high);
quicksort(sortingArray, low, partitionIndex-1);
quicksort(sortingArray, partitionIndex+1, high);
}
}
int partition(int array[], int low, int high) {
int pivot = array[high];
int i = (low-1);
for(int j = low; j <= high-1; j++){
if(array[j] < pivot) {
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i+1], &array[high]);
return (i+1);
}
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}