-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_sort.cpp
55 lines (48 loc) · 958 Bytes
/
quick_sort.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
#include <cstdio>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <list>
#include <set>
#include <map>
using namespace std;
// worst case - O(n ^ 2)
// average case - O(n log n).
// main thing does not take extra memory.
const int N = 10010;
int a[N];
// select pivot and do partition according to them.
inline int doPartition(int l, int r) {
int i = l - 1, pvt = a[r];
for (int j = l; j < r; j++) {
if (a[j] <= pvt) {
swap(a[++i], a[j]);
}
}
swap(a[++i], a[r]);
return i;
}
inline void quickSort(int l, int r) {
if (l < r) {
int q = doPartition(l, r);
quickSort(l, q - 1);
quickSort(q + 1, r);
}
}
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", a + i);
}
quickSort(0, n - 1);
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
}