# 09-排序2 Insert or Merge (25分)

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 NN (\le 100≤100). Then in the next line, NN integers are given as the initial sequence. The last line contains the partially sorted sequence of the NN 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

AC代码:

```#include <iostream>
using namespace std;

bool Check(int a[],int b[],int N)
{
bool flag = true;
for ( int i = 0 ; i < N ; i++){
if ( a[i] != b[i]){
flag = false;
break;
}
}
return flag;
}

void Print(int a[],int N)
{
cout<<a;
for (int i = 1 ; i < N ; i++){
cout<<" "<<a[i];
}
}

bool Insert_Sort(int a[],int b[],int N)
{
bool isInsert = false;
for ( int i = 1 ; i < N ; i++){
int tmp = a[i];
int j;
for (j = i ; j > 0 && a[j-1] > tmp ; j--){
a[j] = a[j-1];
}
a[j] = tmp;
if (isInsert){
cout<<"Insertion Sort"<<endl;
Print(a,N);
break;
}else if(Check(a,b,N)){
isInsert = true;
}
}
return isInsert;
}

void Merge(int a[],int left ,int right , int rightend)
{
int tmp[rightend-left+1];
int leftend = right-1;
int len = rightend - left +1;
int i = left , j = right ,k = 0;
while(i <= leftend && j <= rightend){
if( a[i] <= a[j]){
tmp[k++] = a[i++];
}else{
tmp[k++] = a[j++];
}
}

while(i <= leftend){
tmp[k++] = a[i++];
}
while(j <= rightend){
tmp[k++] = a[j++];
}

for(i = left, k = 0; i <= rightend; i++){
a[i] = tmp[k++];
}
}

bool Merge_Sort(int a[],int b[],int N)
{
bool isMerge = false;
for ( int i = 1 ; i < N ; i *= 2){
for ( int j = 0 ; j < N ; j += i*2){
if ( j + i*2 <= N){
Merge(a,j,j+i,j+i*2-1);
}else if( j + i <= N) {
Merge(a,j,j+i,N-1);
}
}
if (isMerge){
cout<<"Merge Sort"<<endl;
Print(a,N);
break;
}else if (Check(a,b,N)){
isMerge = true;
}
}
return isMerge;
}

int main()
{
int len;
cin>>len;
int *A,*B,*C;
A = new int[len];
B = new int[len];
C = new int[len];
for ( int i = 0 ; i < len ; i++){
cin>>A[i];
C[i] = A[i];
}
for ( int i = 0 ; i < len ; i++){
cin>>B[i];
}

bool isMerge = Merge_Sort(A,B,len);
if ( isMerge){
return 0;
}else{
bool isInsert = Insert_Sort(C,B,len);
return 0;
}

return 0;
} ```

0 条评论

• ### 历届试题 最大子阵

问题描述 　　给定一个n*m的矩阵A，求A中的一个非空子矩阵，使这个子矩阵中的元素和最大。

• ### 寻找大富翁

题目描述 浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. 输入描述: 输入包含多组测试用例. 每个用例首先包含2个...

• ### 09-排序1 排序 (25分)

数据1：只有1个元素； 数据2：11个不相同的整数，测试基本正确性； 数据3：103个随机整数； 数据4：104个随机整数； 数据5：105个随...

• ### LeetCode 310. 最小高度树（图 聪明的BFS，从外向内包围）

对于一个具有树特征的无向图，我们可选择任何一个节点作为根。图因此可以成为树，在所有可能的树中，具有最小高度的树被称为最小高度树。给出这样的一个图，写出一个函数找...

• ### Wannafly挑战赛27

给出长度为n的序列a, 求有多少对数对 (i, j) (1 <= i < j <= n) 满足 ai + aj 为完全平方数。

• ### LeetCode Weekly Contest 32解题思路

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### POJ 3207 Ikki's Story IV - Panda's Trick(2-SAT)

Description liympanda, one of Ikki’s friend, likes playing games with Ikki. Toda...

• ### P1629 邮递员送信

题目描述 有一个邮递员要送东西，邮局在节点1.他总共要送N-1样东西，其目的地分别是2~N。由于这个城市的交通比较繁忙，因此所有的道路都是单行的，共有M条道路，...

• ### HUST 1017 - Exact cover

Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 7636 Solved: 38...

• ### 挑战程序竞赛系列（3）：2.3需要思考的动规

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

### 活动推荐 