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<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ; int origin[maxn],tmpOri[maxn],change[maxn]; bool Insertion(int n);
void showArray(int arr[], int n);
bool Issame(int A[], int B[], int n);
void merge(int n); int main()
{
int n;
scanf("%d",&n); for (int i = ; i < n; i++)
{
scanf("%d",&origin[i]);
tmpOri[i] = origin[i];
}
for (int i = ; i < n; i++)
{
scanf("%d",&change[i]);
} if (Insertion(n))
{
printf("Insertion Sort\n");
showArray(tmpOri,n);
}
else
{
printf("Merge Sort\n");
for (int i = ; i < n; i++)
{
tmpOri[i] = origin[i];
}
merge(n);
} return ;
} bool Insertion(int n)
{
bool flag = false;
for (int i = ; i < n; i++)
{
if (i != && Issame(tmpOri, change, n))
{
flag = true;
} int tmp = tmpOri[i];
int j = i;
while (j >= && tmpOri[j - ] > tmp)
{
tmpOri[j] = tmpOri[j-];
j--;
}
tmpOri[j] = tmp; if (flag)
{
return flag;
}
}
return false;
} void showArray(int arr[], int n)
{
for (int i = ; i < n; i++)
{
printf("%d", arr[i]);
if (i < n - )
{
printf(" ");
}
}
} bool Issame(int A[], int B[], int n)
{
for (int i = ; i < n; i++)
{
if (A[i] != B[i])
{
return false;
}
}
return true;
} void merge(int n)
{
bool flag = false;
for (int step = ; step / <= n; step *= )
{
if (step != && Issame(tmpOri, change, n))
{
flag = true;
} for (int i = ; i < n; i += step)
{
sort(tmpOri+i, tmpOri+min(i+step, n));
} if (flag)
{
showArray(tmpOri, n);
return ;
}
}
}

09-排序2 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. 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 ...

  3. A1089 Insert or Merge (25 分)

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

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

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

  5. 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, and gr ...

  7. 7-19(排序) 寻找大富翁 (25 分)(归并排序)(C语言实现)

    7-19(排序) 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁. 输入格式 ...

  8. pat1089. Insert or Merge (25)

    1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...

  9. PAT 1089. Insert or Merge (25)

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

随机推荐

  1. Vue.js 源码分析(十一) 基础篇 过滤器 filters属性详解

    Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持).过滤器应该被添加在 JavaScrip ...

  2. Spring Security 入门—内存用户验证

    简介 作为 Spring 全家桶组件之一,Spring Security 是一个提供安全机制的组件,它主要解决两个问题: 认证:验证用户名和密码: 授权:对于不同的 URL 权限不一样,只有当认证的用 ...

  3. Blend 设置通明窗体

    原文:Blend 设置通明窗体 这篇开始到下周周一 更新WPF blend 和MVVM模式的博客  因为新入职的公司主要去做WPF的工作 一边整理学习  一遍回顾   但是CSDN 限制每天10篇  ...

  4. ScheduledThreadPoolExecutor使用指南

    ScheduledThreadPoolExecutor是Timer的多线程实现版本,JDK官方推荐使用.ScheduledThreadPoolExecutor用于替代Timer.是接口Schedule ...

  5. 12、Render函数

    1.什么是Render函数 Vue 推荐在绝大多数情况下使用模板来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力.这时你可以用渲染函数,它比模板更接近编译器. ...

  6. html页面的渲染And<script>位置的影响

    周末加班敲代码的时用到了<script>标签,突然想到了一个问题:别的自测项目里面<script>我把他放在了不同位置,这里应该会对代码的执行与渲染后影响吧?于是今天专门进行了 ...

  7. 操作系统与进程.md

    目录 1. 操作系统 1.1 作用 1.2 操作系统的发展 2. 进程的理论 2.1 相关名词 2.2 进程的创建 2.3 进程的状态: 1. 操作系统 ​ 管理.控制.协调计算机硬件与软件资源的计算 ...

  8. Python基础10

    字符串大小写转换,除了upper,lower,还有一种方法,casefold( ) 方法 比较这两种方法的适用范围

  9. 【转】LockSupport深入浅出

    原文:https://www.cnblogs.com/qingquanzi/p/8228422.html 本篇是<自己动手写把"锁">系列技术铺垫的最后一个知识点.本篇 ...

  10. 【数据库】数据库入门(四): SQL查询 - SELETE的进阶使用

    集合操作常用的集合操作主要有三种:UNION(联合集).INTERSECT(交叉集).EXCEPT(求差集).以上三种集合的操作都是直接作用在两个或者多个 SQL 查询语句之间,将所有的元组按照特定的 ...