1089 Insert or Merge (25 point(s))
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 Nnumbers. 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
考察插入排序和归并排序,更关键的是get本题的解题套路!
我的归并排序总结:https://blog.youkuaiyun.com/coderwait/article/details/89344613
我的插入排序总结:https://blog.youkuaiyun.com/coderwait/article/details/89336339
自然而然的想法是编写代码重现这两个算法,然后比对。但实际上,我们并不关心排序算法的实际过程,只关心每一次迭代后产生的最终结果。
启发来自:https://blog.youkuaiyun.com/richenyunqi/article/details/81150510
题中归并排序的方法是把每个元素看成一个子列,在每一次迭代中,依次将相邻的子列合并起来,如两个两个地、四个四个地。这和我所熟悉的不同。因此最开始连样例2都看不懂。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<memory.h>
using namespace std;
int a[101];
int b[101];
int target[101];
int N;
bool isEqual(int arr[]){
for(int i=0;i<N;i++){
if(arr[i]!=target[i]) return false;
}
return true;
}
void output(int arr[]){
for(int i=0;i<N;i++){
if(i==0) cout<<arr[i];
else cout<<" "<<arr[i];
}
}
int main(void){
cin>>N;
for(int i=0;i<N;i++) cin>>a[i];
for(int i=0;i<N;i++) cin>>target[i];
memcpy(b,a,sizeof(a));
bool isInsert = false;
for(int i=2;i<=N;i++){
sort(a,a+i);
if(isEqual(a)){
cout<<"Insertion Sort"<<endl;
sort(a,a+i+1);
output(a);
isInsert = true;
break;
}
}
if(!isInsert){
bool flag = false;
for(int i=2;i<=N;i=i*2){
for(int j=0;j<N;j+=i){
sort(b+j,j+i>=N?b+N:b+j+i);
}
if(flag){
cout<<"Merge Sort"<<endl;
output(b);
break;
}
if(isEqual(b)){
flag = true;
}
}
}
return 0;
}