-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11931.cpp
67 lines (51 loc) · 1.37 KB
/
11931.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
60
61
62
63
64
65
66
67
#include<iostream>
#include<cstdio>
using namespace std;
int arr[1001010], N;
int tmp[1001010];
void quickSort(int low, int high){
if(low >= high) return;
int pivot = low;
int i = low+1, j = high;
while(i <= j){
while(arr[i]>arr[pivot] && i<=high) i++;
while(arr[j]<=arr[pivot] && j>low) j--;
if(i > j) swap(arr[j], arr[pivot]);
else swap(arr[i], arr[j]);
}
quickSort(low, j-1);
quickSort(j+1, high);
}
void insertionSort(){
int pivot;
for(int i=0; i<N-1; i++){
pivot = i;
for(int j=i+1; j<N; j++) if(arr[j] > arr[pivot]) pivot = j;
swap(arr[pivot], arr[i]);
}
}
void merge(int low, int mid, int high){
int i = low, j = mid, tmp_idx = low;
while(i<mid && j<high){
if(arr[i] > arr[j]) tmp[tmp_idx++] = arr[i++];
else tmp[tmp_idx++] = arr[j++];
}
while(i<mid) tmp[tmp_idx++] = arr[i++];
while(j<high) tmp[tmp_idx++] = arr[j++];
for(int i=low; i<high; i++) arr[i] = tmp[i];
}
void mergeSort(int low, int high){
if(low == high-1) return;
int mid = (low+high)/2;
mergeSort(low, mid);
mergeSort(mid, high);
merge(low, mid, high);
}
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio();
cin >> N;
for(int i=0; i<N; i++) cin >> arr[i];
mergeSort(0, N);
for(int i=0; i<N; i++) cout << arr[i] << '\n';
}