-
Notifications
You must be signed in to change notification settings - Fork 0
/
1089.cpp
105 lines (97 loc) · 1.67 KB
/
1089.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
98
99
100
101
102
103
104
105
#include <cstdio>
#include <algorithm>
using namespace std;
int insert_sort(int A[], int n, int B[]){
int count = 0;
for(int i = 0; i < n; i++){
int temp = A[i];
int j = 0;
while(A[j] < A[i] && j < i){
j++;
}
for(int k = i; k > j; k--){
A[k] = A[k-1];
}
A[j] = temp;
if(count == n){
return 1;
}
count = 0;
for(int l = 0; l < n; l++){
if(A[l] == B[l]){
count++;
}
}
}
return -1;
}
void merge(int A[], int L1, int R1, int L2, int R2){
int B[100];
int i = L1, j = L2, index = 0;
while(i <= R1 && j <= R2){
if(A[i] <= A[j]){
B[index++] = A[i++];
}
else{
B[index++] = A[j++];
}
}
while(i <= R1){
B[index++] = A[i++];
}
while(j < R2){
B[index++] = A[j++];
}
for(int i = 0; i < index; i++){
A[L1+i] = B[i];
}
}
int merge_sort(int A[], int B[], int n){
int count = 0;
for(int step = 2; step/2 <= n; step *= 2){
for(int i = 0; i < n; i += step){
int mid = i + step /2 - 1;
if(mid + 1 < n){
merge(A, i, mid, mid + 1, min(i + step - 1, n - 1));
}
}
if(count == n){
return 1;
}
count = 0;
for(int l = 0; l < n; l++){
if(A[l] == B[l]){
count++;
}
}
}
return -1;
}
int main(){
int n, A[110], A1[110], B[110];
scanf("%d", &n);
for(int i = 0; i<n; i++){
scanf("%d", &A[i]);
A1[i] = A[i];
}
for(int i = 0; i<n; i++){
scanf("%d", &B[i]);
}
int is = insert_sort(A, n, B);
if(is == 1){
printf("Insertion Sort\n");
for(int i = 0; i < n - 1; i++){
printf("%d ", A[i]);
}
printf("%d\n", A[n-1]);
}
int ms = merge_sort(A1, B, n);
if(ms == 1){
printf("Merge Sort\n");
for(int i = 0; i < n - 1; i++){
printf("%d ", A1[i]);
}
printf("%d\n", A1[n-1]);
}
return 0;
}