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

解答

插入排序从前向后有序,堆排序从后向前有序……

//
//  main.c
//  Insertion or Heap Sort
//
//  Created by 余南龙 on 2016/12/8.
//  Copyright © 2016年 余南龙. All rights reserved.
//

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int N, i, j, tmp, largest;
    ], ];

    scanf("%d", &N);
    ; i < N; i++) {
        scanf("%d", initial + i);
    }
    ; i < N; i++) {
        scanf("%d", partial + i);
    }
    ; i < N; i++){
        ]){
            break;
        }
    }
    ){
        printf("Heap Sort\n");
        ; i >= ; i--) {
            ]){
                break;
            }
        }
        tmp = partial[i];
        ];
        ] = tmp;
        j = ;
        ) {
            largest = j;
             +  < i&& + ] > partial[largest]){
                largest = j *  + ;
            }
             +  < i&& + ] > partial[largest]){
                largest = j *  + ;
            }
            if (largest != j) {
                tmp = partial[j];
                partial[j] = partial[largest];
                partial[largest] = tmp;
                j = largest;
            }
            else{
                break;
            }
        }
    }
    else{
        printf("Insertion Sort\n");
        tmp = partial[i];
        i--;
        ){
            if (partial[i] > tmp){
                ] = partial[i];
                i--;
            }
            else{
                break;
            }
        }
        ] = tmp;
    }
    ; i < N; i++) {
         == i) {
            printf("%d", partial[i]);
        }
        else{
            printf(" %d", partial[i]);
        }
    }
}

PTA Insertion or Heap Sort的更多相关文章

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

  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. Insertion or Heap Sort

    7-14 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one inp ...

  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. 关于Python的数据分析工具

    Python - 核心编程环境NumPy/SciPy - 用于快速.高效的数组和矩阵运算IPython - 用于Python的可视化交互开发matplotlib - 用于数据的图形可视化pandas ...

  2. Linux系统下如何查看已经登录用户

    查看用户的操作 系统管理员若想知道某一时刻用户的行为,只需要输入命令W 即可,在SHELL终端中输入如下命令: [ROOT@LOCALHOST ROOT] # W 2:31PM UP 11 DAY , ...

  3. Sublime Text 3 引用插件

    汉化插件 点击 View> Show Console 输入import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b ...

  4. JQ判断复选框是否选中

    方法一: if($('#checkbox').is(':checked')) {} 方法二:if ($('#checkbox').attr('checked')) {} 方法三:if ($(" ...

  5. Maven依赖版本冲突的分析及解决小结

    1:前言 做软件开发这几年遇到了许多的问题,也总结了一些问题的解决之道,之后慢慢的再遇到的都是一些重复性的问题了,当然,还有一些自己没有完全弄明白的问题.如果做的事情是重复的,遇到重复性问题的概率也就 ...

  6. 从 A/Looper: Could not create epoll instance. errno=24 错误浅谈解决各种 bug 的思路

    今天代码写着写着就莫名闪退了,手机也没有“程序停止运行”的提示,logcat也没有看到蓝色的调用栈log,这样的闪退最是蛋疼了,还好必现.复现几次之后,终于从logcat中看到了一行可疑的log: A ...

  7. js原型基础

    js中的原型链是实现继承等面向对象的一种方式,而我这里不详细展开,仅仅是画了一张原型部分结构图,主要说明: 1._proto_和prototype之间的关系 2.原型链的起点是Object.proto ...

  8. AspNetPager分页控件

    AspNetPager分页控件解决了分页中的很多问题,直接采用该控件进行分页处理,会将繁琐的分页工作变得简单化,下面是我如何使用AspNetPager控件进行分页处理的详细代码:1.首先到www.we ...

  9. modelsim(3) - summary ,issue,tips

    1) the OEM of modelsim is 10 times slower than offical questa 2)how to the file full path in the mod ...

  10. python使用代理ip发送http请求

    一.需求背景 网站刷票时,经常会遇到限制一个ip只能投票一次的限制,为此需要使用代理ip 二.脚本如下: 1.Proxy_http.py使用代理ip发送httpr的get和post请求 #coding ...