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:

  1. 10
  2. 3 1 2 8 7 5 9 4 6 0
  3. 1 2 3 7 8 5 9 4 6 0

Sample Output 1:

  1. Insertion Sort
  2. 1 2 3 5 7 8 9 4 6 0

Sample Input 2:

  1. 10
  2. 3 1 2 8 7 5 9 4 6 0
  3. 6 4 5 1 0 3 2 7 8 9

Sample Output 2:

  1. Heap Sort
  2. 5 4 3 1 0 2 6 7 8 9
  3.  
  4. //直接插入排序和堆排序
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define N 120
  4. int n,a[N],b[N],heap[N];
  5. int flag = ;
  6. bool same(int *a,int *b){
  7. for(int i=;i<=n;i++) {
  8. if(a[i]!=b[i]) return false;
  9. }
  10. return true;
  11. }
  12. void insert_sort(int *a){
  13. for(int i =;i<=n;i++){//2不是1,因为堆排序输入的可能也是第一次排好的
  14. sort(a+,a+i+);
  15. if(flag!=) break;
  16. if(same(a,b)) flag =;
  17. }
  18. }
  19. void downjust(int l,int h){
  20. int i =l;
  21. int j =*i;
  22. while(j<=h){
  23. if(j+<=h&&heap[j]<heap[j+]){
  24. j=j+;
  25. }
  26. if(heap[j]>heap[i]){
  27. swap(heap[i],heap[j]);
  28. i=j;
  29. j=*i;
  30. }
  31. else
  32. break;
  33. }
  34. }
  35. void create(int *a){
  36. for(int i =n/;i>=;i--){
  37. downjust(i,n);
  38. }
  39. }
  40. void heap_sort(int *a){
  41. for(int i=n;i>;i--){
  42. swap(heap[i],heap[]);
  43. downjust(,i-);
  44. if(flag!=) break;
  45. if(same(heap,b)) flag =;
  46. }
  47. }
  48. int main(){
  49. scanf("%d",&n);
  50. for(int i =;i<=n;i++)
  51. {
  52. scanf("%d",&a[i]);
  53. heap[i] = a[i] ;
  54. }
  55. for(int i=;i<=n;i++){
  56. scanf("%d",&b[i]);
  57. }
  58. insert_sort(a);
  59. create(heap);
  60. heap_sort(heap);//heap
  61. if(flag==){
  62. printf("Insertion Sort\n");
  63. for(int i =;i<=n;i++) {
  64. printf("%d%c",a[i],i==n?'\n':' ');
  65. }
  66. }
  67. else if(flag ==){
  68. printf("Heap Sort\n");
  69. for(int i =;i<=n;i++) {
  70. printf("%d%c",heap[i],i==n?'\n':' ');
  71. }
  72. }
  73. return ;
  74. }

PAT 1098的更多相关文章

  1. PAT 1098. Insertion or Heap Sort

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

  2. PAT甲级1098. Insertion or Heap Sort

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

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

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

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

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

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

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

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

  7. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  8. PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

    题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...

  9. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

随机推荐

  1. redux有价值的文档

    使用 Redux 管理状态,第 1 部分 https://www.ibm.com/developerworks/cn/web/wa-manage-state-with-redux-p1-david-g ...

  2. org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19

    在Maven上部署Struts2时,突然发生了如下错误,但是tomcat还能运行,发送错误的原因时Struts2的版本太高,和tomcat7不兼容. 开始Struts2用了2.5.20版本,后来换成2 ...

  3. Docker中Maven私服的搭建

    为何用到Maven私服? 在实际开发中,项目中可能会用到第三方的jar.内部通讯的服务接口都会打入到公司的私服中. 我们从项目实际开发来看: 一些无法从外部仓库下载的构件,例如内部的项目还能部署到私服 ...

  4. java 8 学习三(Stream API)

    集合讲的是数据,流讲的是计算. 流的数据处理功能支持类似于数据库的操作,以及函数式编程语言中的常用操作,如filter. map. reduce. find. match. sort等. 流操作可以顺 ...

  5. LeetCode 702. Search in a Sorted Array of Unknown Size

    原题链接在这里:https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/ 题目: Given an integer ...

  6. netflix conductor 学习(一)docker-compose 运行

    为了方便学习以及使用netflix conductor 基于官方的dockerfile,构建了server 以及ui 的容器镜像并push dockerhub 环境准备 官方docker-compos ...

  7. ksh与bash的异同

    (1) 在ksh是,数组的index只能从0到1023,而bash中没有这样的限制. (2) ksh与bash初始化数组的语法不同: 如下所示 icymoon# ksh icymoon# set -A ...

  8. js处理事件冒泡(兼容写法)

    event = event || window.event; if (event.stopPropagation) { event.stopPropagation(); } else { event. ...

  9. 安装supervisor 失败 :/usr/bin/python: bad interpreter: No such file

    以前在安装python 双版本时将python改为了python2所以找不到python,打开那个echo_supervised_conf然后把 #!/usr/bin/python 改为如图就可以了

  10. IDE 问题及解决

    目录 Eclipse 篇 1.MarketPlace 打不开,对话框闪退 2.使用 lombok ,预编译不通过 3.Eclipse + PyDev - > Unresolved import: ...