-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
55 lines (41 loc) · 994 Bytes
/
quickSort.cpp
File metadata and controls
55 lines (41 loc) · 994 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
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int arr[1000000];
int calPivot(int start, int end) {
return (start + end) / 2;
}
int partition(int start, int end) {
int pivot = calPivot(start, end);
int pivotValue = arr[pivot];
swap(arr[pivot], arr[end]);
int storedIndex = start;
for(int i = start; i < end; i++) {
if(arr[i] < pivotValue) {
swap(arr[i], arr[storedIndex]);
storedIndex++;
}
}
swap(arr[storedIndex], arr[end]);
return storedIndex;
}
void quickSort(int start, int end) {
if(start < end) {
int pivot = partition(start, end);
quickSort(start, pivot - 1);
quickSort(pivot + 1, end);
}
}
int main() {
int num;
scanf("%d", &num);
for(int i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}
quickSort(0, num-1);
for(int i = 0; i < num; i++) {
printf("%d\n", arr[i]);
}
return 0;
}