-
Notifications
You must be signed in to change notification settings - Fork 2
/
B1035.cpp
83 lines (76 loc) · 1.36 KB
/
B1035.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
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 110;
int origin[MAXN], tempOri[MAXN], middle[MAXN], N;
bool isSame(int a[], int b[]){
for(int i = 1; i <= N; ++i){
if(a[i] != b[i])
return false;
}
return true;
}
void init(){
for(int i = 1; i <= N; ++i){
tempOri[i] = origin[i];
}
}
bool insertSort(){
init();
bool flag = false;
for(int i = 2; i <= N; ++i){
if(i != 2 && isSame(tempOri, middle)){
flag = true;
}
int j = i, temp = tempOri[i];
while(j > 1 && tempOri[j-1] > temp){
tempOri[j] = tempOri[j-1];
--j;
}
tempOri[j] = temp;
if(flag == true){
return true;
}
}
return false;
}
bool mergeSort(){
init();
bool flag = false;
for(int step = 2; step / 2 <= N; step *= 2){
if(step != 2 && isSame(tempOri, middle)){
flag = true;
}
for(int i = 1; i <= N; i += step){
sort(tempOri+i, tempOri+min(i+step, N+1));
}
if(flag == true){
return true;
}
}
return false;
}
void showArray(int a[]){
for(int i = 1; i <= N; ++i){
if(i != 1) printf(" ");
printf("%d", a[i]);
}
printf("\n");
}
int main(){
scanf("%d", &N);
for(int i = 1; i <= N; ++i){
scanf("%d", &origin[i]);
}
for(int i = 1; i <= N; ++i){
scanf("%d", &middle[i]);
}
if(insertSort()){
printf("Insertion Sort\n");
showArray(tempOri);
}else if(mergeSort()){
printf("Merge Sort\n");
showArray(tempOri);
}
return 0;
}