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.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

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 "Heap 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 resulting 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 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9
 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int A[];
int B[];
int L;
void Swap(int *C,int i, int j)
{
int temp = C[i];
C[i] = C[j];
C[j] = temp;
}
void Print(int N)
{
int i;
for (i = ; i < N - ; i++)
printf("%d ", B[i]);
printf("%d", B[i]);
} int Charge(int N)
{
int i;
if (B[] > B[] && B[] > B[])
return ;
else
return ;
} void Insert_Once(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 PrecDown(int i,int N)
{
int Parent, Child;
int Tmp = A[i];
for (Parent = i; Parent * + <= N; Parent = Child)
{
Child = Parent * + ;
if (Child != N && A[Child] < A[Child + ])
Child++;
if (Tmp >= A[Child])break;
else
A[Parent] = A[Child];
}
A[Parent] = Tmp;
}
void BuildMaxHeap(int N)
{
for (int i = (N - ) / ; i >= ; i--)
PrecDown(i, N - );
}
void Heap_Sort(int N)
{
for (int i = N - ; i >= ; i--)
{
Swap(A, , i);
PrecDown(, i-);
}
}
int FindI(int N)
{
int i;
for (i = N - ; i >= && A[i] == B[i]; i--);
return i;
}
void PrecDown_Once(int i)
{
Swap(B,, i);
int Parent, Child;
int tmp;
tmp = B[];
for (Parent = ; (Parent * ) + <= i-; Parent = Child)
{
Child = (Parent * ) + ;
if (Child != i-&& B[Child]<B[Child + ])
Child++;
if (tmp >= B[Child])break;
else
B[Parent] = B[Child];
}
B[Parent] = tmp;
}
void Heap_Once(int N)
{
BuildMaxHeap(N);
Heap_Sort(N);
int i = FindI(N);
PrecDown_Once(i);
}
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(N))
printf("Heap Sort\n");
else
printf("Insertion Sort\n");
for (int i = ; i < N - ; i++)
if (B[i] <= B[i + ]) //这里必须是大于等于
OrderPosition = i + ;
else
break;
if (Flag)
Heap_Once(N);
else
Insert_Once(OrderPosition + , N);
Print(N);
return ;
}

1098 Insertion or Heap Sort (25分)的更多相关文章

  1. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

  2. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  3. PTA 09-排序3 Insertion or Heap Sort (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort   (25分) Accor ...

  4. PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

  5. 1098. Insertion or Heap Sort (25)

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

  6. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  7. PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

    题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...

  8. 09-排序3 Insertion or Heap Sort (25 分)

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

  9. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

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

随机推荐

  1. java算法--链表

    虽然这个文章看着很多,但是大多是对于细节的讲解,如果想要快速了解,可以直接观看末尾代码.上面的代码内容都是来自于文章末尾的代码. 很重要的算法,也是比较简单的算法. 但是在java中,因为不存在c和c ...

  2. ASP.NET Core身份认证服务框架IdentityServer4 介绍

    IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架.它可以在您的应用程序中提供以下功能: 它使你的应用程序具有如下特点: 认证即服务 适 ...

  3. Redis系列六 - 浅谈如何设计秒杀系统

    前言 设计一个系统之前,我们肯定要先确认系统业务场景是怎样的,下面就以某电商平台上的秒杀活动为场景,一起来探讨一个秒杀系统改如何去设计. 场景 我们现在要卖100件纸尿布,按照系统的用户量及以往经验来 ...

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

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

  5. 配置ubunto 流量使用限制 python 实现简单 http server

    很多ubunto 都有流量限制,使用流量.如每天使用200M ,超过了就要提示信息 原理,在本机 开一个 http 服务, 显示错误信息,哪流量使用完以后,使用 iptables 将 流量转发到 本机 ...

  6. 数据库中的两个最重要的日志redo log和binlog

    mysql整体来看其实只有两部分,一部分是server层,一部分是引擎层. 1.redo log(重做日志):当有一条记录需要更新的时候,InnoDB 引擎就会先把记录写入redo log里面,并更新 ...

  7. vs2017 dlib19.3 opencv3.41 C++ 环境配置 人脸特征点识别

    身为一个.net程序员经过两天的采坑终于把人脸特征检测的项目跑通了,然后本文将以dlib项目中人脸特征检测工程为例,讲解dlib与opencv 在vs2017 C++ 项目中的编译与运行路径配置. 1 ...

  8. 74. pNextID、pNextVal、pNID的区别

    pNextID是平台调用单个新增组件的时候调用的: pNextVal是平台批量新增的时候调用: pNID应该是自己写的 :

  9. Spring Boot + LayUI 批量修改数据 数据包含着对象

    页面展示 HTML 代码 <blockquote class="layui-elem-quote demoTable"> <div class="lay ...

  10. ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory

    问题描述: ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory 首先检 ...