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. 【01】openLayers 第一个地图

    效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  2. nes 红白机模拟器 第7篇 编译使用方法

    模拟器,基于 InfoNES ,作者添加修改以下功能: 1, joypad 真实手柄驱动程序(字符型设备驱动) 2,原始图像只有256*240 ,添加 图像放大算法,这里实现了2种,a, 最近邻插值 ...

  3. ASP.NET Core ActionFilter引发的一个EF异常

    最近在使用ASP.NET Core的时候出现了一个奇怪的问题.在一个Controller上使用了一个ActionFilter之后经常出现EF报错. InvalidOperationException: ...

  4. ggplot2(9) 数据操作

    9.1 plyr包简介 ddply {plyr}: Split data frame, apply function, and return results in a data frame. ddpl ...

  5. Python - 常用内置变量

    直接上代码 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 这是注释__doc__会打印这部分内容 "&qu ...

  6. 【Weiss】【第03章】练习3.26:双端队列

    [练习3.26] 双端队列(deque)是由一些项的表组成的数据结构,对该数据结构可以进行下列操作: Push(X,D):将项X插入到双端队列D的前端. Pop(D):从双端队列D中删除前端项并返回. ...

  7. 广告行业中那些趣事系列7:实战腾讯开源的文本分类项目NeuralClassifier

    摘要:本篇主要分享腾讯开源的文本分类项目NeuralClassifier.虽然实际项目中使用BERT进行文本分类,但是在不同的场景下我们可能还需要使用其他的文本分类算法,比如TextCNN.RCNN等 ...

  8. Java-方法(新手)

    //创建的一个类.public class zy1ri0319 { //公共静态的主方法. public static void main(String[] args){ //调用方法. zy1(); ...

  9. jsp(3,6,9) EL表达式及JSTL

    1. jsp 1.1jsp是什么 全称: Java Server Pages,java服务器页面.和Servlet一样,是sun公司定义的一种动态网页开发技术.    特点:基于html模版,可以在h ...

  10. Python-练习 while 和for 循环

    # while 循环 age = 56count = 0 while count < 3 : guess_age=int(input('输入年龄:')) if guess_age == age: ...