1098 Insertion or Heap Sort (25分)
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分)的更多相关文章
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- 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 ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 09-排序3 Insertion or Heap Sort (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
随机推荐
- NSFileHandle的用法(用于读写文件)
利用NSFilehandle类提供的方法,允许更有效地使用文件. 一般而言,处理文件时都要经历以下三个步骤: 1.打开文件,并获取一个NSFileHandle对象,以便在后面的I/O操作中引用该文件 ...
- git的日常使用(补课)
使用git的一些反响 如果在github上做一个仓库来使用的话... 首先手动在github上创建一个空的仓库,默认没有任何东西的 使用git的命令 git pull 创建的github仓库地址 能把 ...
- C++ 小练习,一个整型数字的处理
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> //输入一个任意位数的int整数,并判断该整数的位数,并输出每一位数(每个数字中间用空 ...
- Spring Ioc 依赖查找
Spring ioc 有依赖查找和依赖注入,之前不太明白依赖查找是什么意思,翻了一大堆博客看了好多定义也不太清楚 ,后来看了小马哥视频,他通过代码演示,清楚地讲解了什么是 依赖查找以及几种依赖查找的方 ...
- 一起了解 .Net Foundation 项目 No.20
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. System.Drawin ...
- javaScript 基础知识汇总 (十)
1.New Function 语法:let func = new Function ([arg1[, arg2[, ...argN]],] functionBody) //无参数示例: let say ...
- 建议14:灵活使用Arguments
Javascript支持Arguments机制.Arguments是一个为数组,可以通过数组下标形式获取该集合中传递给函数的参数值.例如:下面函数中,没有指定形参,但在函数体内通过Arguments对 ...
- HTML每日学习笔记(2)
7.16.2019 1.html表单:用于得到用户不同类型的输入 <form>元素定义表单: <form> First name:<br> <input ty ...
- 死磕Lambda表达式(四):常用的函数式接口
失去人性,失去很多:失去兽性,失去一切.--<三体> 在Java8支持Lambda表达式以后,为了满足Lambda表达式的一些典型使用场景,JDK为我们提供了大量常用的函数式接口.它们主要 ...
- linux入门系列18--Web服务之Apache服务2
接上一篇文章,在了解Apache基本配置以及SELinux相关知识后,继续演示Apache提供的虚拟主机功能以及访问控制方式. 如果还没看上一篇的建议先查看后再来,上篇文章"linux入门系 ...