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. java线程锁基础

    定义运行方法 package com.company; // 包名import java.util.concurrent.locks.ReentrantLock;import java.util.co ...

  2. Android模拟器太慢怎么办?使用微软的VS模拟器

    开发过android的人都知道,android模拟器非常的慢,推荐使用微软的VS模拟器. (1)到 https://visualstudio.microsoft.com/zh-hans/vs/msft ...

  3. thinkphp3.2.3集成phpexcel1.8导出设置单元格合并

    1 到这里下载classes里面的文件 https://github.com/PHPOffice/PHPExcel 2 然后放到 thinkphp的vendor 新建一个文件夹 Phpexcel  然 ...

  4. Vue 项目中遇到的跨域问题及解决方法

    原文:https://www.jb51.net/article/137278.htm 问题描述 前端 vue 框架,跨域问题后台加这段代码 header("Access-Control-Al ...

  5. Asp.Net Core采用MailKit部署到Linux Docker连接邮件服务器报错

    前段时间看文章了解到发邮件的SmtpClient已经过时了,微软官方推荐大家用其他解决方案,例如MailKit. https://docs.microsoft.com/zh-cn/dotnet/api ...

  6. 基于串口的SD_card系统

    概述 基于串口的SD_card系统1, 扫描文件:2, 新建文件:3, 删除文件:4, 写入文件:5, 读取文件. 整个文件系统的串口通信方式都是ASC通信方式. 文件系统分为简单实用方式和专业使用方 ...

  7. 在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)

    在上篇文章介绍了Web Api中使用令牌进行授权的后端实现方法,基于WebApi2和OWIN OAuth实现了获取access token,使用token访问需授权的资源信息.本文将介绍在Web Ap ...

  8. c#在sqlserver中使用EF框架

    vs2017,sqlserver2017(localdb)调试通过.在sqlserver中创建数据库d1,表t1如下: 录入数据如下: 在vs新建任意项目,此处以控制台为例.添加数据模型Model1: ...

  9. 华为 鸿蒙系统(HarmonyOS)

    HarmonyOS Ⅰ. 鸿蒙系统简介 鸿蒙系统(HarmonyOS),是第一款基于微内核的全场景分布式OS,是华为自主研发的操作系统.2019年8月9日,鸿蒙系统在华为开发者大会<HDC.20 ...

  10. android 第三方开源库 学习汇总

    依赖注入框架ButterKnife  https://github.com/JakeWharton/butterknife  学习过程     专注于android的View注入框架,并不支持其他方面 ...