PTA Insertion or Heap Sort
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的更多相关文章
- 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 ...
- 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 ...
- Insertion or Heap Sort
7-14 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one inp ...
- 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 ...
随机推荐
- 修改Oracle字符集为 ZHS16GBK
SQL*Plus: Release 11.2.0.1.0 Production on Wed Aug 10 13:12:48 2016 Copyright (c) 1982, 2009, Oracle ...
- su with hyphen and without - su带横杠和不带横杠
The difference between "-" and "no hyphen" is that the latter keeps your existin ...
- Oracle基础笔记
=====================================第一章:oracle数据库基础============================================= Or ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- 组合or继承
面向对象设计有一个原则“优先使用对象组合,而不是继承”. 下面是两者优缺点的比较: 组 合 关 系 继 承 关 系 优点:不破坏封装,整体类与局部类之间松耦合,彼此相对独立 缺点:破坏封装,子类与父类 ...
- Runtime的几个小例子(含Demo)
一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.) 1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数); [runti ...
- Salesforce 数据清洗
新系统上线后,需要导入历史数据,但是旧数据格式,数据缺失,数据错误,奇异值,属性归类与新系统有很大的gap.因此我们需要建立一套数据动态清洗规则给Salesforce系统,通过这些规则自动清洗导入数据 ...
- Tcp/IP
在数据传输完毕之后,通信双方都可以发出释放连接的请求.释放连接的过程为如上图所示: 1)数据传输结束后,主机A的应用进程先向其TCP发出释放连接请求,不在发送数据.TCP通知对方要释放从A ...
- ajax通讯之格式详解
前言: ajax的出现,一定程度上改变了js的命运,同时也被广泛使用,而jq的兴起也大大降低了ajax的使用难度.虽然,jq的ajax方法使用起来十分便利,但是大部分开发人员也仅仅只是对其中的几个属性 ...
- javascript 闭包最简单理解
首先说3点与闭包有关系的东西. 一.变量的作用域 变量的作用域不难理解. 1.函数内部可以访问函数外部的变量,而函数外部不能访问函数内部的变量. 2.如果在函数内定义变量的时候,不加var,那么是全局 ...