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的更多相关文章

  1. PTA Insertion or Heap Sort

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

  2. 1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...

  3. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

  4. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  5. pat1098. Insertion or Heap Sort (25)

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

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

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

  7. 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 ...

  8. PAT_A1098#Insertion or Heap Sort

    Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: According to Wikipedia: Insertion sort  ...

  9. 1098 Insertion or Heap Sort——PAT甲级真题

    1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...

随机推荐

  1. UltraEdit - 怎么显示文件标签栏和侧边栏

    显示文件标签栏 view -> views/lists -> open Files Tabs 显示侧边栏 view -> views/lists -> File Tree Vi ...

  2. 关于 typedef struct 和 struct

    举个例子说明:typedef struct abc{ int x; int y; int z;}ABC;是将结构体abc类型重新起个名字为ABC,以后再定义同一类型的变量时,可以写成:ABC m,n; ...

  3. jquery $.trim()方法的介绍

    http://www.jb51.net/article/50282.htm

  4. 项目上线后出现Bug,该如何处理?

    项目在上线之后又出现了Bug,这让很多测试人员和开发人员头痛.但很多时候线上Bug普遍地存在,不可避免. 任何项目都存在未发现 Bug  和 已发现 Bug  两种情况,不存在没有 Bug的情况. 即 ...

  5. Caffe实战三(依赖包解析及环境配置)

    前面的文章使用的软件环境是开始时通过apt-get命令所安装的,本文将通过编译源码的方式重新配置一个可迁移的软件环境.(参考:<深度学习 21天实战Caffe> 第五天 Caffe依赖包解 ...

  6. 封装jQuery插件实现TAB切换

    先上效果图: 直接上代码: index.html <!DOCTYPE html> <html lang="en"> <head> <met ...

  7. 寻找项目中顶级Vue对象 (一)

    个人博客首发博客园: http://www.cnblogs.com/zhangrunhao/ 参考 感谢作者 从一个奇怪的错误出发理解 Vue 基本概念 安装 - Vue.js 渲染函数 - Vue. ...

  8. Dapper系列之一:Dapper的入门(多表批量插入)

    Dapper介绍  简介:      不知道博客怎么去写去排版,查了好多相关博客,也根据自己做过项目总结,正好最近搭个微服务框架,顺便把搭建微服务框架所运用的知识都进行博客梳理,为了以后复习,就仔细琢 ...

  9. <meta>详解

    一.元数据和<meta> 元数据是描述以提供关于其他数据的数据,在<meta>中,html document是被描述的数据,meta标签中包括的数据是描述html docume ...

  10. Linux 合并多个txt文件到一个文件

    Linux 或 类Unix 下实现合并多个文件内容到一个文件中 代码如下 cat b1.txt b2.txt b3.txt > b_all.txt 或者 cat *.txt > merge ...