09-排序3 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 (≤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 "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
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn = ;
int tempOri[maxn], origin[maxn], change[maxn];
int n; bool Insertion();
bool isSame(int A[], int B[]);
void showArray(int A[]);
void Heap();
void downjust(int low, int high); int main()
{ scanf("%d",&n);
for (int i = ; i <= n; i++)
{
scanf("%d",&origin[i]);
tempOri[i] = origin[i];
} for (int i = ; i <= n; i++)
{
scanf("%d",&change[i]);
} if (Insertion())
{
printf("Insertion Sort\n");
showArray(tempOri);
}
else
{
printf("Heap Sort\n");
for (int i = ; i <= n; i++)
{
tempOri[i] = origin[i];
}
Heap();
} return ;
} bool Insertion()
{
bool flag = false;
for (int i = ; i <= n; i++)
{
if (i != && isSame(tempOri, change))
{
flag = true;
} sort(tempOri,tempOri+i+);
if (flag)
{
return true;
}
}
return false;
} bool isSame(int A[], int B[])
{
for (int i = ; i <= n; i++)
{
if (A[i] != B[i])
{
return false;
}
}
return true;
} void showArray(int A[])
{
for (int i = ; i <= n; i++)
{
printf("%d",A[i]);
if (i < n)
{
printf(" ");
}
}
} void Heap()
{
bool flag = false;
for (int i = n/; i >= ; i--)
{
downjust(i,n);
} for (int i = n; i > ; i--)
{
if (i != n && isSame(tempOri, change))
{
flag = true;
}
swap(tempOri[i], tempOri[]);
downjust(, i-); if (flag)
{
showArray(tempOri);
return;
}
}
} void downjust(int low, int high)
{
int parent = low, child = parent * ;
while (child <= high)
{
if (child < high && tempOri[child+] > tempOri[child])
{
child++;
} if (tempOri[child] > tempOri[parent])
{
swap(tempOri[child], tempOri[parent]);
parent = child;
child *= ;
}
else
{
break;
}
}
}
09-排序3 Insertion or Heap Sort (25 分)的更多相关文章
- 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甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 1098 Insertion or Heap Sort (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- pat1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 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 ...
随机推荐
- 多年老项目添加cocoapod管理之后的各种问题解决方案
整个组件化过程中遇到的问题及解决方案原文出处 hehuoya.com pod install 报警告(debug.release..) 解决方案:other link flags : $(inheri ...
- Intellij IDEA的安装教程
一.下载安装 1.打开官网:http://www.jetbrains.com/idea/,点击页面中的“DOWNLOAD” 2.根据自己的需要选择下载的IntelliJ IDEA版本,此处我的电脑是W ...
- spring的15个经典面试题
总结Spring框架的15个经典面试题. 什么是Spring框架? Spring是一种轻量级框架,旨在提高开发人员的开发效率以及系统的可维护性. 我们一般说的Spring框架就是Spring Fram ...
- 创建Maven项目时,GroupId和Artifact Id该怎么填写呢?
1.什么是groupid和artifactId? groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根 ...
- Kafka学习笔记3--Kafka的生产者和消费者配置
下载解压 kafka 后,在 kafka/config 下有 3 个配置文件与主题及其生产.消费相关. server.properties--服务端配置 producer.properties--生产 ...
- 《即时消息技术剖析与实战》学习笔记6——IM系统如何保证消息的安全性
在消息产生.流转的各个环节中,需要保证消息传输安全性.消息存储安全性.消息内容安全性. 一.消息传输安全性 消息传输的重要防范点有两个,一是访问入口安全,二是传输链路安全. 1.HttpDNS保证访问 ...
- AspNetCore.Identity详解2——注册用户
上一篇:AspNetCore.Identity详解1——入门使用 打开数据库,可以看到使用EF自动生成的表结构如下: 重点关注AspNetUsers表,打开数据库里的表可以知道目前也只用到了这张表.然 ...
- Git分支和版本回退
一.分支 1.分支简单介绍 简单使用: 可以将git branch new_branch和git checkout new_branch两个命令合并成一个命令: git checkout -b new ...
- Eureka重要对象简介
在进行分析EurekaClient和EurekaServer之间通信的源码之前,我们首先需要熟悉一下几个实体类 InstanceInfo 这个类代表着EurekaClient实例,客户端向服务端请求注 ...
- ES6 Set和Map集合(六)
一.Set集合创建Set实例:let set = new Set();1.特性:a.Set本身是一个构造函数,用来生成Set数据结构[类比数组结构]b.Set函数可以接受具有Iterable接口的数据 ...