-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1089.cpp
97 lines (88 loc) · 1.66 KB
/
A1089.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 110;
int initial[MAXN];
int partial[MAXN];
int ans[MAXN];
int N;
void init(){
for(int i = 0; i < N; ++i){
ans[i] = initial[i];
}
}
void print(){
for(int i = 0; i < N; ++i){
if(i != 0) printf(" ");
printf("%d", ans[i]);
}
}
bool judge(){
for(int i = 0; i < N; ++i){
if(ans[i] != partial[i]) return false;
}
return true;
}
void insertion(int i){
int j = i - 1;
int tmp = ans[i];
while(j >= 0 && ans[j] > tmp){
ans[j+1] = ans[j];
--j;
}
ans[j+1] = tmp;
}
bool insert_sort(){
init();
for(int i = 1; i < N; ++i){
insertion(i);
if(judge()){
insertion(i+1);
return true;
}
}
return false;
}
void merge(int L1, int R1, int L2, int R2){
int i = L1, j = L2, tmp[MAXN], index = 0;
while(i <= R1 && j <= R2){
if(ans[i] <= ans[j]) tmp[index++] = ans[i++];
else tmp[index++] = ans[j++];
}
while(i <= R1) tmp[index++] = ans[i++];
while(j <= R2) tmp[index++] = ans[j++];
for(i = 0; i < index; ++i){
ans[L1 + i] = tmp[i];
}
}
bool merge_sort(){
init();
for(int step = 2; step / 2 < N; step *= 2){
for(int i = 0; i < N; i += step){
int mid = i + step/2 - 1;
merge(i, mid, mid + 1, min(i + step - 1, N-1));
}
if(judge()){
step *= 2;
for(int i = 0; i < N; i += step){
int mid = i + step/2 - 1;
merge(i, mid, mid + 1, min(i + step - 1, N-1));
}
return true;
}
}
return false;
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%d", &initial[i]);
}
for(int i = 0; i < N; ++i){
scanf("%d", &partial[i]);
}
if(insert_sort()) printf("Insertion Sort\n");
else if(merge_sort()) printf("Merge Sort\n");
print();
return 0;
}