-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
44 lines (38 loc) · 865 Bytes
/
Copy pathQuickSort.cpp
File metadata and controls
44 lines (38 loc) · 865 Bytes
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
#include <iostream>
using namespace std;
inline void Swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
inline int partition(int *A, int st, int dr) {
int lo = st - 1, hi = dr - 1;
for (int i = st; i <= hi; i++) {
if (A[i] <= A[dr]) {
lo++;
Swap(&A[lo], &A[i]);
}
}
Swap(&A[lo + 1], &A[dr]);
return lo + 1;
}
inline void quickSort(int *A, int st, int dr) {
if (st < dr) {
int pivot = partition(A, st, dr);
quickSort(A, pivot + 1, dr);
quickSort(A, st, pivot - 1);
}
}
int n, v[100005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++)
cin >> v[i];
quickSort(v, 0, n - 1);
for (int i = 0; i < n; i++)
cout << v[i] << ' ';
cout << '\n';
return 0;
}