According to Wikipedia:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.
Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in the first line either "Insertion Sort" or "Merge Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input 1:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
Sample Output 1:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
Sample Input 2:
10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6
Sample Output 2:
Merge Sort
1 2 3 8 4 5 7 9 0 6
题目大意:给定一个原始序列和由某排序算法产生的中间序列,请你判断该算法是插入算法还是归并算法。首先在第1行中输出"Insertion Sort"表示插入排序、或"Merge Sort"表示归并排序;然后在第2行中输出用该排序算法再迭代一轮的结果序列。
分析:题目保证一定是插入排序或者归并排序,不会出现歧义。可以分别按照插入排序和归并排序模拟,如果不是插入排序,则一定是归并排序。
对于插入排序,每次插入一个数字,再与中间序列比较。当两个序列相同时,说明是插入排序。
对于归并排序,注意每次排序是按照趟数来的,不会出现前一半排序了后一半无序的情况。比如
原序列: 3 1 2 8 7 5 9 4 0 6
中间序列:1 3 2 8 5 7 4 9 0 6
中间序列是以2为大小的一趟归并。下一次排序应该以4为大小进行一趟归并。
cpp
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;
int cmp(int *a1,int *a2,int n)
{
for(int i=0;i<n;++i)
if(a1[i]!=a2[i])return 0;
return 1;
}
bool insert_judge(int *arr,int *num,int n)
{
for(int i=1;i<n;++i)
{
sort(arr,arr+i+1);
if(cmp(arr,num,n))
{
printf("Insertion Sort\n");
sort(arr,arr+i+2);
for(int i=0;i<n;++i)
if(!i)printf("%d",arr[i]);
else printf(" %d",arr[i]);
return 1;
}
}
return 0;
}
void merge_judge(int *arr,int *num,int n)
{
for(int i=2;i<n;i=min(i*2,n))
{
for(int j=0;j<n;j+=i)
{
sort(arr+j,arr+min(j+i,n));
}
if(cmp(arr,num,n))
{
printf("Merge Sort\n");
i=min(i*2,n);
for(int j=0;j<n;j+=i)
{
sort(arr+j,arr+min(j+i,n));
}
for(int i=0;i<n;++i)
if(!i)printf("%d",arr[i]);
else printf(" %d",arr[i]);
return;
}
}
return;
}
int main(void)
{
#ifdef test
freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
clock_t start=clock();
#endif //test
int n,f=1;scanf("%d",&n);
int num1[n+5],num2[n+5],cnt[n+5];
for(int i=0;i<n;++i)scanf("%d",&num1[i]),cnt[i]=num1[i];
for(int i=0;i<n;++i)scanf("%d",&num2[i]);
f=insert_judge(num1,num2,n);
if(!f)merge_judge(cnt,num2,n);
#ifdef test
clockid_t end=clock();
double endtime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\n\n\n\n\n");
cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位
cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位
#endif //test
return 0;
}