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 ...
随机推荐
- flash builder4.7 for Mac升级AIRSDK详解
使用flash builder 打包ANE时或者打包ipa时候常常会遇到AIRSDK版本低的问题,然而flash builder4.7默认使用的AIRSDK是3.4而flash builder4.7 ...
- typedef 揭秘
typedef用来声明一个别名,typedef关键字后面是一个声明.语法上typedef属于存储类声明说明符 一.引言 如果你理解typedef的简单形式: typedef int PARA; 这种形 ...
- 移动端WEB页面
百度前端技术学院第一阶段任务十一,关于移动端WEB页面布局,参考资料如下(都是一些网页链接): MDN:手机网页开发 MDN:在移动浏览器中使用viewport元标签控制布局 移动前端开发和 Web ...
- c#修改config中的AppSettings属性
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cfa.AppS ...
- angularjs1 实现地图添加自定义控件(搜索功能)及事件
// 添加地图自定义控件的事件 function addEventHandler(target, eventName, handler) { if (target.addEventListener) ...
- CentOS6上无法启动NFS服务
CentOS6上无法启动NFS服务 1.系统环境 物理机:Windows 7(32位)旗舰版 虚拟机:CentOS 6.5 2.问题描述 安装好nfs应用程序之后,打算启动nfs服务,却出现以下的报错 ...
- C# DataGridView显示行号的三种方法
方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dgGrid_RowPostPaint( obj ...
- ubuntu 'Unable to correct problems, you have held broken packages' 错误
在用apt 安装软件时,有时会用国内的源以加快下载速度. 但是在使用ubuntu 14.04的过程中,这一过程可能会导致错误“Unable to correct problems, you have ...
- rabbitMQ学习(七)反馈模式
反馈模式 在消费端接收到消息后,会反馈给服务器信息. 连接代码: import java.io.IOException; import com.rabbitmq.client.Channel; imp ...
- Python安装mysql-python错误提示python setup.py egg_info
做python项目,需要用到mysql,一般用python-mysql,安装时遇到错误提示如下: Command "python setup.py egg_info" failed ...