参考这里写的http://www.cnblogs.com/kylehz/p/4134915.html
#include <stdio.h>
#include <algorithm>
using namespace std;
#define MAX 200
int n;
int initial1[MAX],initial2[MAX];
int given[MAX];
int flag1=0,flag2=0;
bool cmp(int a[],int b[]){
for (int i=0; i<n; i++) {
if (a[i]!=b[i]) {
return false;
}
}
return true;
}
void insert(){
for (int i=2; i<=n; i++) {
sort(initial1,initial1+i);
if (cmp(initial1, given)) {
flag1=1;
}
if (flag1&&!cmp(initial1, given)) {
return;
}
}
flag1=0;
}
void merge(){
for (int i=2; i<=n; i*=2) {//每次mergesort的长度。
int j;
for (j=0; j+i<=n; j+=i) {
sort(initial2+j, initial2+j+i);//这里要注意一下不要越界
}
sort(initial2+j, initial2+n);
if (cmp(initial2, given)) {
flag2=1;
}
if (flag2&&!cmp(initial2, given)) {
return ;
}
}
flag2=0;
}
void output(char * str,int arr[]){
printf("%s\n",str);
for (int i=0; i<n-1; i++) {
printf("%d ",arr[i]);
}
printf("%d\n",arr[n-1]);
}
int main(){
//freopen("/Users/pantingting/Documents/code/data/input", "r", stdin);
scanf("%d",&n);
for (int i=0; i<n; i++) {
scanf("%d",&initial1[i]);
initial2[i]=initial1[i];
}
for (int j=0; j<n; j++) {
scanf("%d",&given[j]);
}
insert();
merge();
if (flag1==1)
output("Insertion Sort",initial1);
if (flag2==1)
output("Merge Sort",initial2);
return 0;
}