题目地址

https://pta.patest.cn/pta/test/16/exam/4/question/676

5-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 NN (\le 100≤100). Then in the next line, NN integers are given as the initial sequence. The last line contains the partially sorted sequence of the NN 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
/*
评测结果
时间  结果  得分  题目  编译器 用时(ms)    内存(MB)    用户
2017-07-06 18:04    答案正确    25  5-14    gcc 14  1
测试点结果
测试点 结果  得分/满分   用时(ms)    内存(MB)
测试点1    答案正确    7/7 1   1
测试点2    答案正确    6/6 2   1
测试点3    答案正确    2/2 1   1
测试点4    答案正确    2/2 2   1
测试点5    答案正确    4/4 14  1
测试点6    答案正确    4/4 14  1
*/
#include<stdio.h>
#define MAXN 100
int A[MAXN],B[MAXN],tmp[MAXN];
int gNextTurnToPrint=0;
int gPrinted=0;
int gSortType=0;
void swap(int *a,int *b)
{int temp;temp=*a;*a=*b;*b=temp;
}
void DBG_printarray(int N) //调试用函数
{int d;for(d=0;d<N;d++){printf("%d ",A[d]);}printf("\n");
}
void CheckMethod(int N)
{if(gPrinted)return;int i;if (gNextTurnToPrint==1){if(gSortType==0)printf("Insertion Sort\n");elseprintf("Heap Sort\n");for(i=0;i<N;i++){printf("%d",A[i]);if(i!=N-1)printf(" ");}gNextTurnToPrint=0;gPrinted=1;return;}for (i=0;i<N;i++){if(A[i]!=B[i])return;}gNextTurnToPrint=1;
}void InsertionSort(int a[],int left ,int right,int N)
{int i,j,temp;for(i=left;i<right;i++){temp=a[i+1];for(j=i+1;j>left;j--){if(temp<a[j-1])a[j]=a[j-1];else break;}a[j]=temp;CheckMethod(N);}
}void PrecDown(int a[],int x,int N)
{int temp=a[x];int parent,child;parent=x;for(child=2*parent+1; child<= N ;child=child*2+1){if(child!=N){if(a[child+1]>a[child])child++;}if(temp<a[child]){a[parent]=a[child];parent=child;}else break;}a[parent]=temp;
//  DBG_printarray(N+1);
}void HeapSort(int a[],int N)
{//建大顶堆然后把首元素后移int i,ptrRight=N-1,temp,d;for(i=(ptrRight-1)/2;i>=0;i--){PrecDown(a,i,ptrRight);}for(i=ptrRight;i>0;i--){temp=a[i];a[i]=a[0];a[0]=temp;PrecDown(a,0,i-1);CheckMethod(N);}
}int main()
{int i,N;scanf("%d",&N);for(i=0;i<N;i++){scanf("%d",&A[i]);tmp[i]=A[i];}for(i=0;i<N;i++){scanf("%d",&B[i]);}InsertionSort(A,0,N-1,N);if(!gPrinted){for(i=0;i<N;i++){A[i]=tmp[i];}gSortType=1;HeapSort(A,N);}}

  

转载于:https://www.cnblogs.com/gk2017/p/7141134.html

PTA 09-排序3 Insertion or Heap Sort (25分)相关推荐

  1. 1098 Insertion or Heap Sort (25 分)【难度: 中 / 插入排序 堆排序 堆排序不会未完成】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805368847187968 堆排好久没写了不会写,有时间补

  2. PAT甲级1098 Insertion or Heap Sort:[C++题解]堆排序和插入排序

    文章目录 题目分析 题目链接 题目分析 分析 插入排序的特点:前半部分有序,后半部分保持原序. 堆排序的特点: 后半部分有序,前半部分无序. 本题的bug在于,答案是唯一的,即不是插入排序就是堆排序, ...

  3. PAT A1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort 分数 25 作者 CHEN, Yue 单位 浙江大学 According to Wikipedia: Insertion sort iterat ...

  4. 1098 Insertion or Heap Sort 需再做

    1. 应该还做过一道类似的题目,也是要求判断属于哪种排序的中间过程,并要求写出下一轮排序结果,这次的进步是上来就知道用向量存数据,这样方便直接比较,而且下标0不能存元素,因为堆排序的堆是一个完全二叉树 ...

  5. C++学习之路 | PTA(天梯赛)—— L2-007 家庭房产 (25分)(带注释)(并查集)(精简)

    L2-007 家庭房产 (25分) 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤1000),随后N行,每行按 ...

  6. PTA 数据结构与算法分析 7-40 奥运排行榜 (25 分)

    7-40 奥运排行榜 (25 分) 每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如中国金牌总数列第一的时候,中国媒体就公布"金牌榜":而美 ...

  7. PTA 01-复杂度2 Maximum Subsequence Sum (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum   (25分) Given ...

  8. java heap排序_关于Java排序算法-堆排序(Heap Sort)

    堆排序是利用堆的特性进行排序的过程. 堆排序:输出堆顶的最小(大)值后,使剩余的n-1个元素序列重新再建成堆,则可得到原序列的次小(大)值.反复进行可得到一个有序序列,整个过程称为堆排序. 堆排序分为 ...

  9. PTA甲级 1097 Deduplication on a Linked List (25分)-链表处理

    文章目录 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生词如下: 题目大意: 思路如下: ...

最新文章

  1. rabbitmq的整体架构一览
  2. Java 和 HTTP 的那些事(四) HTTPS 和 证书
  3. 汇编语言--CMOS RAM芯片
  4. Linux man指令
  5. PHP中的常见魔术方法功能作用及用法实例
  6. 设置主机名 centos redhad7
  7. 【myeclipse】java.lang.NullPointerException at com.genuitec.eclipse.ast.deploy.core.Deployment
  8. . SQL多条件查询存储过程
  9. 解决PHP导出大量数据时设置超链接的问题 --mxp
  10. 三、Spring的@Scope设置组件作用域
  11. 在sqlyog进行数据库的备份_狂神说MySQL07:权限及如何设计数据库
  12. 计算机科学与python编程导论_【基于Python】MIT OCW 计算机科学与编程导论
  13. 机器学习的归宿《终极算法》·一
  14. TeamTalk 单服务端配置
  15. Matlab实现身份证号码快速识别
  16. 三极管工作原理_三极管的工作原理是什么,详解三极管工作原理。
  17. MobiCom2019几篇有意思的文章
  18. 盘点5款常用的网络拓扑图制作工具
  19. Nagios如何配置告警短信?
  20. IAR 设置TAB代码格式问题

热门文章

  1. Oracle 解决4031错误
  2. 两个月计算机考研数学,管综数学考前两个月让你事半功倍的方法
  3. Android自定义弹窗模仿微信,Android仿微信右上角点击加号弹出PopupWindow
  4. What‘s new in dubbo-go v1.5.6
  5. 如何使用 Istio 进行多集群部署管理:多控制平面
  6. 从零开始入门 K8s | Kubernetes API 编程范式
  7. 迅为linux下串口,迅为iMX6UL开发板多路串口开发板接口详解
  8. Postman工具之参数化
  9. mysql 数据库编译安装_mysql 数据库 编译安装(千峰)
  10. (一)机器学习数据处理