-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.cpp
executable file
·59 lines (46 loc) · 973 Bytes
/
quicksort.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
55
56
57
58
59
// https://www.acmicpc.net/submit/2751
// O(n log n), Worst case: O(n^2)
#include <bits/stdc++.h>
using namespace std;
void quicksort(int, int);
void swap(int, int);
int arr[1'000'000];
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
for (int i = 0; i<n; ++i) {
cin>>arr[i];
}
quicksort(0,n);
for (int i = 0; i<n; ++i) {
cout << arr[i] << '\n';
}
return 0;
}
void quicksort(int left, int right)
{
if (left>=right) {
return;
}
swap(left,(left+right)/2);
int last = left;
for (int i = left+1; i<right; ++i) {
if (arr[left]>arr[i]) {
swap(++last,i);
}
}
swap(left,last);
quicksort(left,last);
quicksort(last+1,right);
return;
}
void swap(int i, int j)
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
return;
}