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. Oracle - 坏块修复(一)

    一.概述 本文将介绍如何模拟坏块,以及出现坏块该如何修复.实验分为以下几个步骤. 1. 表出现坏块 2. 索引出现坏块 二.环境准备 本实验都是在oracle 11G归档模式下进行. 1. 准备相关表 ...

  2. 基于osg的python三维程序开发(三)------几何形体及纹理

    def createScene(): geode = osg.Geode() pointsGeom = osg.Geometry() vertices = osg.Vec3Array() vertic ...

  3. 通过实现简单聊天室了解websocket的基础使用

    websocket基础使用 用到的依赖包 websocket的依赖 <dependency> <groupId>javax.websocket</groupId> ...

  4. 测试必知必会系列- Linux常用命令 - tar

    21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 压缩一 ...

  5. Posix线程编程指南(4)

    Posix线程编程指南(4) 杨沙洲 原文地址:http://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part4/ 线程终 ...

  6. Event loops秒懂

    Event loops秒懂 简介 JS是一种单线程脚本语言,为什么要设计成单线程? 举例说明,假设JS是多线程脚本语言,A线程修改了DOM,B线程删除了DOM,一旦B线程先执行完,DOM被删除了,A线 ...

  7. [C++]HelloWorld背后的故事!

    人物介绍 姓名 HelloWorld 性别 .cpp 住址 D:\ 身份证号(SHA1) 25106D2879A9EA300BB264F8155A71D7C44DA2E8 故事简介 编写源文件 预编译 ...

  8. ajax结合sweetalert弹出框删除数据

    思路:

  9. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  10. Selenium系列(十一) - 针对两种上传文件方式的实现方案

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...