-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1035.cpp
More file actions
43 lines (40 loc) · 1.13 KB
/
Copy path1035.cpp
File metadata and controls
43 lines (40 loc) · 1.13 KB
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
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int N;
int A1[101], A2[101]; // 原始序列A1 中间序列A2
int i, j;
cin>>N;
for (i=0; i<N; i++) cin>>A1[i];
for (i=0; i<N; i++) cin>>A2[i];
for (i=0; A2[i]<=A2[i+1] && i<N-1; i++) ; // i作为有序序列最后一个元素下标退出循环
for (j=++i; A1[j]==A2[j] && j<N; j++ ) ; // A1 A2从 第一个无序的元素开始 逐一比对
if (j==N) {// 前半部分有序而后半部分未改动可以确定是插入排序
cout<<"Insertion Sort"<<endl;
sort(A1, A1+i+1);
}
else {
cout<<"Merge Sort"<<endl;
int k = 1;1
int flag=1; //用来标记是否归并到 “中间序列”
while (flag)
{
flag = 0;
for (i=0; i<N; i++)
if (A1[i]!=A2[i])
flag = 1;
k*=2;
for (i=0; i<N/k; i++)
sort(A1+i*k, A1+(i+1)*k);
for (i=k*(N/k); i<N; i++) // 对 非偶数序列的“尾巴”进行排序
sort(A1+k*(N/k), A1+N);
}
}
cout<<A1[0];
for (i=1; i<N; i++)
cout<<" "<<A1[i];
cout<<endl;
return 0;
}