分析

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

分析

题目大意就是给出一组初始数和一组排序中间过程的数,判断中间过程的这组数是由插入排序还是归并排序得来的
基本思路是:实现归并排序和插入排序,排一次比较一次,如果排序数和中间过程的数相等了,再做一轮后输出

#include<iostream>
using namespace std; 

bool judge(int A[],int B[],int N){
   for(int i=0;i<N;i++)
   	if(A[i]!=B[i])
   		return false;
   return true;
}

// 插入排序 
bool Insertion_Sort(int A[],int B[],int N){
   for(int P=1;P<N;P++){
   	int tmp = A[P];
   	int j = P;
   	for(;j>0 && tmp < A[j-1];j--)
   		A[j] = A[j-1];
   	A[j] = tmp;
   	if(judge(A,B,N)){  // 如果相等了,再做一轮 
   		P++;
   		int tmp = A[P];
   		int j = P;
   		for(;j>0 && tmp < A[j-1];j--)
   			A[j] = A[j-1];
   		A[j] = tmp;
   		return false;
   	}
   }
   return true; 
}


void Merge(int A[],int tmpA[],int L,int R,int RightEnd){
   // L = 左边起始位置,R = 右边起始位置,RightEnd = 右边终点位置 
   int NumSize = RightEnd-L+1;   // 排序个数 
   int LeftEnd = R-1;   // 左边终止位置 
   int tmp = L; 
   // 排序 
   while(L <= LeftEnd && R <= RightEnd){
   	if(A[L] <= A[R])
   		tmpA[tmp++] = A[L++];
   	else
   		tmpA[tmp++] = A[R++];
   } 
   // 如果左边有剩 
   while(L <= LeftEnd)
   	tmpA[tmp++] = A[L++];
   // 如果右边有剩 
   while(R <= RightEnd)
   	tmpA[tmp++] = A[R++];
   // 导回 A 数组 
   for(int i=0;i<NumSize;i++)
   	A[RightEnd--] = tmpA[--tmp];
}

void Merge_pass(int A[],int tmpA[],int N,int length){
   int i;
   // 每次 2*length 为一组排序单元 
   for(i=0;i<=N-2*length;i+=2*length)
   	Merge(A,tmpA,i,i+length,i+length*2-1);
   // 处理剩下的不够一组的排序单元 
   if(i+length < N)   // 如果左边够了,但是右边不齐,再次进入排序 
   	Merge(A,tmpA,i,i+length,N-1);
   else   // 如果左边都不够,直接导给 tmpA 
   	for(int j=i;j<N;j++)
   		tmpA[j] = A[j];			
}

// 归并排序
bool Merge_Sort(int A[],int B[],int N){
   int tmpA[N];
   int length = 1;
   while(length < N){
   	Merge_pass(A,tmpA,N,length);  // 一趟归并 
   	length *=2;
   	if(judge(A,B,N)){   // 如果相等了,再做一轮 
   		Merge_pass(A,tmpA,N,length);
   		return false;
   	}
   } 
   return true;
}

// 输出 
void output(int A[],int N){
   for(int i=0;i<N;i++){
   	if(i)
   		cout<<" ";
   	cout<<A[i]; 
   } 
} 

int main(){
   int N;
   cin>>N;
   int A[N],tmpA[N];
   for(int i=0;i<N;i++){
   	cin>>A[i];
   	tmpA[i] = A[i];
   }
   int B[N];
   for(int i=0;i<N;i++)
   	cin>>B[i];
   // 用 tmpA 数组做归并排序,判断是否是归并排序 
   if(!Merge_Sort(tmpA,B,N)){
   	cout<<"Merge Sort"<<endl;
   	output(tmpA,N);
   	return 0;
   }
   // 用 A 数组做插入排序,判断是否是插入排序
   if(!Insertion_Sort(A,B,N)){
   	cout<<"Insertion Sort"<<endl;
   	output(A,N);
   	return 0;
   }
   return 0;
}