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
#define MaxSize 100
int a[MaxSize];
int res[MaxSize];
using namespace std;
void GetData(int a[], int N) {
for (int i = 0; i < N; i++) {
cin >> a[i];
}
}
void CopyArr(int a[], int b[], int N) {
/*将a[]数组拷贝到b[]数组,数组大小为N*/
for (int i = 0; i < N; i++) {
b[i] = a[i];
}
}
int CompareRes(int a[], int res[],int N) {
/*比较a[]和res[],相同则返回1,不同则返回0*/
int Flag_IsSame = 1;
for (int i = 0; i < N; i++) {
if (a[i] != res[i]) {
Flag_IsSame = 0;
break;
}
}
return Flag_IsSame;
}
void InsertSort(int tmpArr[], int i) {
/*对tmpArr[]数组做第i次插入排序*/
int tmp = tmpArr[i];
int j;
for (j = i; j > 0 && tmp < tmpArr[j - 1]; j--) {
tmpArr[j] = tmpArr[j - 1];
}
tmpArr[j] = tmp;
}
void MergeArr(int a[], int tmp[], int L,int R,int R_end) {
/*将数组a[],有序区间[L,R-1],[R,R_end]两个区间进行不断归并,然后存入到tmp[]中*/
int L_end = R - 1;
int ptr2tmp=L;
while(L <= L_end&&R <= R_end) {
if (a[L] < a[R]) {
tmp[ptr2tmp++] = a[L++];
}
else {
tmp[ptr2tmp++] = a[R++];
}
}
while (L <= L_end) {
tmp[ptr2tmp++] = a[L++];
}
while (R <= R_end) {
tmp[ptr2tmp++] = a[R++];
}
}
void MergeSort(int a[], int res[], int N, int length) {
/*将长度为N的a[],以length为长度归并,然后存入到tmp中*/
int i;
for (i = 0; i + 2 * length < N; i = i + 2 * length) {
MergeArr(a, res, i, i + length, i + 2 * length - 1);
}
/*剩余有序子列的合并*/
if(i+length> N;
GetData(a, N);//读取原始数据到数组a[]
GetData(res, N);//读取排序后数据到数组res[]
int InOrMeg;
InOrMeg = InsertOrMerge(a, res, N);
DispRes(InOrMeg, res,N);
return 0;
}