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 (≤). 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
 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int A[];
int B[];
int L;
void Print(int N)
{
int i;
for (i = ; i < N - ; i++)
printf("%d ", B[i]);
printf("%d", B[i]);
}
int IsOrder(int B[],int lo, int hi)
{
for (int i = lo; i < hi - ; i++)
if (B[i] > B[i + ])
return ;
return ;
}
int ChargeLength(int B[], int N, int Length) //检查以Length为长度的序列
{
int i;
for (i = ; i < N -Length; i +=Length)
if (!IsOrder(B, i, i+Length))
return ;
if (i< N)
if (!IsOrder(B, i, N))
return ;
L = Length;
return ;
}
int Charge(int B[], int N)
{
int Length = ;
while(Length <=N)
{
if (ChargeLength(B, N, Length))
Length *= ;
else
if (Length > )
return ;
else
return ;
}
} void Insert_Once(int B[],int i,int N)
{
int j;
int Temp = B[i];
for (j = i; j > && B[j - ] >Temp; j--)
B[j] = B[j - ];
B[j] = Temp;
} void Merge(int lo, int mi, int hi)
{
int* B1 = (int*)malloc(sizeof(int) * (mi - lo));
for (int i = ; i < mi - lo; i++)B1[i] = B[lo + i];
int i, j, k;
i = lo;
j = ;
k = mi;
while (j < mi - lo)
{
if (k >= hi || B1[j] <= B[k])B[i++] = B1[j++];
if (k<hi && B1[j]>B[k])B[i++] = B[k++];
}
free(B1);
}
void Merge_Once(int B[], int Length, int N)
{
int i;
for (i = ; i < N - * Length; i += * Length)
Merge(i, i + Length, i + * Length);
if (i + Length < N)
Merge(i, i + Length, N);
} int main()
{
int N;
int Flag = ;
int OrderPosition = ;
scanf("%d", &N);
for (int i = ; i < N; i++)
scanf("%d", &A[i]);
for (int i = ; i < N; i++)
scanf("%d", &B[i]);
if (Flag=Charge(B, N))
printf("Merge Sort\n");
else
printf("Insertion Sort\n");
for (int i = ; i < N - ; i++)
if (B[i] <=B[i + ]) //这里必须是大于等于
OrderPosition = i + ;
else
break;
if (Flag)
Merge_Once(B, L, N);
else
Insert_Once(B, OrderPosition+, N);
Print(N);
return ;
}

1089 Insert or Merge (25分)的更多相关文章

  1. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  2. 1089 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  3. 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)

    题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...

  4. PTA 09-排序2 Insert or Merge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge   (25分) According to ...

  5. PAT 1089. Insert or Merge (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  6. 1089. Insert or Merge (25)

    题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...

  7. 1089. Insert or Merge (25)-判断插入排序还是归并排序

    判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...

  8. PAT (Advanced Level) 1089. Insert or Merge (25)

    简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  9. A1089 Insert or Merge (25 分)

    一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...

随机推荐

  1. 《自拍教程45》Python_adb实时监控Logcat日志

    接上一篇:adb命令_一键截取logcat日志, 有一天, 系统稳定性开发负责人找到我,希望我能在跑android 系统monkey的时候, 实时监控logcat的输出,如果一旦发现"jav ...

  2. 使用 C# 和 OpenGL (SharpGL) 实现的一个简易画图版

    原文地址:https://billc.io/2019/10/fpainter/ 计算机图形学的第一个大作业是用 OpenGL 或 DirectX3d 实现一个平面的画图,应当具备直线和圆形的功能.正好 ...

  3. IPv4地址表示法详解

    在TCP/IP协议中,IP地址是一个最基本的概念,本文就来参考<计算机网络>谢希仁 这本书,总结一下IPv4地址表示法的发展阶段,做个读书笔记. IP地址的编址方法共经过了三个历史阶段: ...

  4. C 2016笔试题

    1.下面程序的输出结果是(    ) int x = 3; do { printf(“%d\n”,x -= 2); }while(!(-- x)); 分析:x初始值为3,第一次循环中运行printf函 ...

  5. maven如何安装oracle驱动jdbc的jar包

    一.因为oracle驱动的压缩包在maven官网上并没有提供,所以需要自己去手动下载. 二.下载安装步骤如下: (1)第一步:找到你本地的oracle驱动包所在位置: (2)在cmd中打开jar包所在 ...

  6. 初始Django—Hello world

    1. 准备环境 > python -V Python > pip -V pip from c:\python3\lib\site-packages\pip (python 3.7) > ...

  7. 判断网站CMS

    1.robots.txt文件 robots.txt文件我们写过爬虫的就知道,这个文件是告诉我们哪些目录是禁止爬取的.但是大部分的时候我们都能通过robots.txt文件来判断出cms的类型 如: 从w ...

  8. C# 基础知识系列- 4 面向对象

    面向对象 面向对象是一个抽象的概念,其本质就是对事物以抽象的方式建立对应的模型. 简单来讲,比如我有一只钢笔,那么我就可以通过分析,可以得到 这只钢笔的材第是塑料,品牌是个杂牌 ,里面装的墨是黑色的, ...

  9. vue封装axios

    一.安装axios npm install --save axios 二.在src下面创建文件夹api=>api.js(接口集合)+http.js(封装的请求) 三.在main.js中引用api ...

  10. JMX(Java Management Extension)学习

    目录 基本概念 MBean的种类 StandardMBean DynamicBean ModelMBean JMX的实现方式 StandardMBean的使用方法 JMX服务的访问方式 JMX--No ...