-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
56 lines (49 loc) · 1.07 KB
/
Copy pathsort.cpp
File metadata and controls
56 lines (49 loc) · 1.07 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
#include <iostream>
using namespace std;
int partition(int* arr, int start, int end) {
int pivot = arr[start];
int low = start + 1;
int high = end;
while (true) {
while (low <= high && arr[low] <= pivot) {low++;}
while (low <= high && arr[high] > pivot) {high--;}
if (low <= high) {
swap(arr[low], arr[high]);
} else {
break;
}
}
swap(arr[start], arr[high]);
return high;
}
void quickSort(int* arr, int start, int end) {
if (start < end) {
int pivotIndex = partition(arr, start, end);
quickSort(arr, start, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, end);
}
}
/**
* 4 3 2 1
* 4 4 2 1
* 3 4 2 1
*
*
*/
int main() {
int n;
cout << "Input n: ";
cin >> n;
int* arr = new int(n);
cout << "Input array:" << endl;
int elem;
for (int i = 0; i < n; i++) {
cin >> elem;
arr[i] = elem;
}
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << ' ';
}
cout << endl;
}