-
Notifications
You must be signed in to change notification settings - Fork 0
/
1035_insert_merge.cpp
143 lines (139 loc) · 2.55 KB
/
1035_insert_merge.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<iostream>
#include"math.h"
using namespace std;
int step=2;
int me_sort(int *src,int deep,int num)
{
if(deep<=1)
{
return 0;
}
int tmp1[step+1];
int tmp2[step+1];
tmp1[step]=1024;
tmp2[step]=1024;
for(int i=0;i<num;)
{
for(int j=0;j<step;j++)
{
tmp1[j]=*(src+i+j);
tmp2[j]=*(src+i+j+step);
}
int *p=tmp1;
int *q=tmp2;
for(int j=0;j<step*2;j++)
{
if(*p<*q)
{
*(src+j)=*p;
p++;
}
else
{
*(src+j)=*q;
q++;
}
}
}
}
void in_sort(int *src,int num)
{
int i;
for(i=num-1;i>=0;i--)
{
if(*(src+i)<*(src+num))
{
break;
}
}
int tmp=*(src+num);
for(int j=num-1;j>i;j--)
{
*(src+j+1)=*(src+j);
}
*(src+i+1)=tmp;
}
int main()
{
int num;
cin>>num;
int src[num];
int det[num];
int in_tmp[num+1];
int me_tmp[num+1];
int choose=0;
int b_flag=1024;
int deep=1;
int deep_num=0;
for(int i=0;i<num;i++)
{
cin>>src[i];
}
for(int i=0;i<num;i++)
{
cin>>det[i];
}
for(int i=0;i<num;i++)
{
in_tmp[i]=src[i];
me_tmp[i]=src[i];
}
for(;deep<=num;deep *=2)
{
deep_num++;
}
for(int i=0;i<num;i++)
{
in_sort(in_tmp,i);
me_sort(me_tmp,deep_num,num);
deep_num--;
int in_flag=0;
int me_flag=0;
if(b_flag==0)
{
break;
}
for(int j=0;j<num;j++)
{
if(det[j]!=in_tmp[j])
{
in_flag=1;
}
if(det[j]!=me_tmp[j])
{
me_flag=1;
}
}
if(in_flag == 0)
{
choose=1;
b_flag=2;
}
if(me_flag == 0)
{
choose=2;
b_flag=2;
}
b_flag--;
}
if(choose==1)
{
int i;
cout<<"Insertion Sort"<<endl;
for(i=0;i<num-1;i++)
{
cout<<in_tmp[i]<<" ";
}
cout<<in_tmp[i];
}
if(choose==2)
{
int i;
cout<<"Merge Sort"<<endl;
for(i=0;i<num-1;i++)
{
cout<<me_tmp[i]<<" ";
}
cout<<me_tmp[i];
}
}