Insertion or Heap Sort
7-14 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 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 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<iostream>
using namespace std;
int main(){
int N; cin>>N;
int i,j,flag=,tag=;
int num0[N],num1[N];
for(i=;i<N;i++)
cin>>num0[i];
for(i=;i<N;i++)
cin>>num1[i];
for(i=;i<N-;i++)
if(num1[i]>num1[i+]) break;
for(j=i+;j<N;j++)
if(num1[j]!=num0[j]) flag=;
if(flag==){
cout<<"Heap Sort"<<endl;
for(j=;j<N;j++)
if(num1[(j-)/]<num1[j]) break;
j=j-;
int temp=num1[j];
num1[j]=num1[];
num1[]=temp;
j--;
int parent,child;
temp=num1[];
for(parent=;*parent+<=j;parent=child){
child=parent*+;
if(child<j&&num1[child+]>num1[child])
child++;
if(num1[child]>temp) num1[parent]=num1[child];
else break;
}
num1[parent]=temp;
}
else{
cout<<"Insertion Sort"<<endl;
int temp=num1[i+];
for(i=i+;i>;i--)
if(num1[i-]>temp) num1[i]=num1[i-];
else break;
num1[i]=temp;
}
for(int k=;k<N;k++)
if(tag++==) cout<<num1[k];
else cout<<" "<<num1[k];
return ;
}
Insertion or Heap Sort的更多相关文章
- PTA Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1098 Insertion or Heap Sort
1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...
- PAT甲级1098. Insertion or Heap Sort
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
- PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...
- 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 ...
- 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_A1098#Insertion or Heap Sort
Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: According to Wikipedia: Insertion sort ...
- 1098 Insertion or Heap Sort——PAT甲级真题
1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...
随机推荐
- Mac 解登录密码Keychain
在终端输入: security unlock-keychain -p "login pwd" ~/Library/Keychains/login.keychain 在制作macOS ...
- jsp include
1.<%@ include file="a.jsp"%> 路径无法动态赋值,只能写成固定路径: 生成一个jsp页面,整个编译 2.<jsp:include pag ...
- Problem E: 穷游中国在统题 优先队列 + 模拟
http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=4 Problem E: 穷游中国在统题 Description Travel ...
- Linux常用命令——tac、bc
1.从文件尾到文件头一页一页的显示内容 tac xxx.log |more //tac命令与cat命令相反,从文件尾开始读文件 2.shell下科学计算工具bc echo "scale=5; ...
- PHP使用iconv函数遍历数组转换字符集
/** * 字符串/二维数组/多维数组编码转换 * @param string $in_charset * @param string $out_charset * @param mixed $dat ...
- postman断言分析
最近测试中用到postman,使用后就简单总结下常用的断言,下面带图的自己最常用的,其他的没怎么用. postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可. 断言 ...
- Sublime折腾记录
本文可以理解为FAQ,主要是为了大家GET一些技能,具体内容包括LICENSE.重置.Package Control的安装,其他内容以后可能补充... 最后说明一下自己的版本:Build 3114 L ...
- EMAC IP 核
在有线连接的世界里,以太网(Ethernet)无所不在.以太网具有各种速度模式.接口方式.以及灵活的配置方式.现在的以太网卡都是10/100/1000Mbps自适应网卡.以太网的物理层(PHY)通常使 ...
- lavarel功能总结
详细可参见笔记:laraval学习笔记(二) 路由 route 绑定模型,绑定参数 模版 blade .blade.php后缀,有laravel自己的模版语法 模型 model 如果用create创建 ...
- easybcd 支持 windows 10 和 ubuntu 14.04 双系统启动
家里计算机系统 windows 10 全新安装. 原本是双系统的,还有一个ubuntu. windows 10 安装以后,恢复ubuntu就是问题了. (事后经验:请不要立刻安装bcd修改工具) 最初 ...