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 ...
随机推荐
- 测不准的程序员(Heisenberg Developers)
摘译自http://mikehadlow.blogspot.co.uk/2014/06/heisenberg-developers.html ——“你无法在不改变他们状态的情况下观察一个开发者” ...
- Android开发-自动更新
为车机写apk,先实现版本的自动更新. 1.不能再主线程中调用会阻塞ui的功能,需要使用异步方式调用网络,引入Android Async Http框架,需要两个包:android-async-http ...
- 统计在从1到n的正整数中1出现的次数
问题: 给定一个十进制正整数N,写下从1开始,到N的所有整数,然后数一下其中出现的所有“1”的个数. 例如:N= 2,写下1,2.这样只出现了1个“1”. N= 12,我们会写下1, 2, 3, 4, ...
- 《从零开始做一个MEAN全栈项目》(3)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 上一篇文章给大家讲了一下本项目的开发计划,这一章将会开始着手搭建一个MEAN项目.千里之行,始于足下, ...
- md5算法
md5算法 不可逆的:原文-->密文.用系统的API可以实现: 123456 ---密文 1987 ----密文: 算法步骤: 1.用每个byte去和11111111做与运算并且得到的是int类 ...
- MySQL 基础语句
MySQL 基础语句 多个知识点 ----------------------------------------------------------------------------------- ...
- 样式:让div里的两个控件在一行的操作
table的td里如果放一个text,希望在右侧再放一个按钮,让这两个控件在一行,但是放了之后总是底部不能对齐,这样的话,加上下边这句样式就可以了 position:relative; top:17p ...
- C#遍历集合与移除元素的方法
如果用foreach,会造成被遍历的集合更改后带来异常问题. 此时,用for循环可有效的解决这个问题. for(int i=0;i<List.Count;i++) { if(条件是真) { Li ...
- Dev ChartControl鼠标移动显示坐标点
his.chartIRC_RATES.MouseMove += new MouseEventHandler(chartIRC_RATES_MouseMove); ToolTipController t ...
- centos,nginx安装备忘
# ssh 登录慢解决 vim /etc/ssh/sshd_config UseDNS no # add limitsvi /etc/security/limits.conf* soft nproc ...