A1098. Insertion or Heap Sort
According to Wikipedia:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At 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 (<=100). 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 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
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int num[], sequ[], num2[], N;
void downAdjust(int adj, int N){
int i = adj, j = * i;
while(j <= N){
if(j < N && num[j] < num[j + ]){
j = j + ;
}
if(num[i] < num[j]){
swap(num[i], num[j]);
i = j;
j = * i;
}else{
break;
}
}
}
int heapSort(int N){
int times = N;
for(int i = N / ; i >= ; i--){
downAdjust(i, N);
}
int find, prt = ;
for(int i = ; i <= times; i++){
find = ;
swap(num[], num[N]);
N--;
downAdjust(, N);
for(int j = ; j <= times; j++){
if(num[j] != sequ[j]){
find = ;
break;
}
}
if(find == ){
prt = ;
continue;
}
if(prt == ){
printf("Heap Sort\n");
for(int k = ; k <= times; k++){
if(k == times) printf("%d", num[k]);
else printf("%d ", num[k]);
}
return ;
}
}
return ;
} void insertSort(int N){
int find, prt = ;
for(int i = ; i < N; i++){
find = ;
int temp = num2[i + ];
int j;
for(j = i; j >= ; j--){
if(temp <= num2[j]){
num2[j + ] = num2[j];
}else break;
}
num2[j + ] = temp;
for(int k = ; k <= N; k++){
if(num2[k] != sequ[k]){
find = ;
break;
}
}
if(find == ){
prt = ;
continue;
}
if(prt == ){
printf("Insertion Sort\n");
for(int k = ; k <= N; k++){
if(k == N) printf("%d", num2[k]);
else printf("%d ", num2[k]);
}
return;
}
}
}
int main(){
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d", &num[i]);
num2[i] = num[i];
}
for(int i = ; i <= N; i++){
scanf("%d", &sequ[i]);
}
int tag = heapSort(N);
if(tag == )
insertSort(N);
cin >> N;
return ;
}
总结:
1、堆排序主要有两部分(大根堆)。
向下调整一个元素A:将A元素与其左右孩子比较,如果它小,则与其中较大的孩子交换。继续追踪这个A元素,在它的调整过的新位置上,如果它比新的孩子节点还要小,则继续交换,直到A大于自己的两个孩子(或者只有一个孩子),或A到达最低端叶节点。
void downAdjust(int adj, int N){  //adj为待调整元素下标,N为数组长度
    int i = adj, j =  * i;
    while(j <= N){
        if(j < N && num[j] < num[j + ]){
            j = j + ;
        }
        if(num[i] < num[j]){
            swap(num[i], num[j]);
            i = j;
            j =  * i;
        }else{
            break;
        }
    }
}
堆排序:初始化先构造一个大根堆,即从最后一个非叶节点开始(下标N/2)直到根节点,都做一次向下调整,就得到初始的大根堆。 若从小到大排序,则将堆顶元素与末端元素交换并将数组长度减一,再对新还上来的堆顶元素进行向下调整,即为一趟堆排序。
int heapSort(int N){
    int times = N;
    for(int i = N / ; i >= ; i--){   //构造初始大根堆
        downAdjust(i, N);
    }
    for(int i = ; i <= times; i++){
        find = ;
        swap(num[], num[N]);
        N--;           //排序区间 -1
        downAdjust(, N);
    }
    return ;
}
2、prt是一次性的,不要在每一趟排序的循环中把将prt置0,这样就无法输出答案了。
3、堆排序下标最好从1开始。
4、注意堆排序的尾部范围每次-1。
A1098. Insertion or Heap Sort的更多相关文章
- PAT A1098 Insertion or Heap Sort (25 分)——堆排序和插入排序,未完待续。。
		According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ... 
- PAT甲级——A1098 Insertion or Heap Sort
		According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ... 
- PTA Insertion or Heap Sort
		According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ... 
- 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 ... 
- 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 ... 
随机推荐
- git客户端下载 和安装
			网址 https://git-scm.com/download/win 点击next 说明: (1)图标组件(Addition icons) : 选择是否创建桌面快捷方式. (2)桌面浏览(Wind ... 
- k8s容器的资源限制
			1.k8s支持内存和cpu的限制 requests:容器运行需求,最低保障limits:限制,硬限制(资源上限) CPU: 1颗逻辑CPU(1核CPU=4个逻辑CPU) 1物理核=1000个微核(mi ... 
- jenkins和jdk版本问题
			问题:公司业务是用的jdk1.7的,但最新版的jenkins (jenkins-2.138.2-1.1.noarch.rpm)却只支持jdk1.8 分析: 1.公司业务用的jdk1.7不能换,不然影响 ... 
- 集合之LinkedList(含JDK1.8源码分析)
			一.前言 LinkedList是基于链表实现的,所以先讲解一下什么是链表.链表原先是C/C++的概念,是一种线性的存储结构,意思是将要存储的数据存在一个存储单元里面,这个存储单元里面除了存放有待存储的 ... 
- vs code配置
			新版的用户设置不是代码, https://blog.csdn.net/zhaojia92/article/details/53862840 https://www.cnblogs.com/why-no ... 
- CodeForces - 1051D Bicolorings(DP)
			题目链接:http://codeforces.com/problemset/problem/1051/D 看了大佬的题解后觉着是简单的dp,咋自己做就做不来呢. 大佬的题解:https://www.c ... 
- luogu3391
			P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ... 
- 【C/C++】递归算法
			所谓递归——函数的递归调用.c语言的这种特性给程序设计带来许多方便.尤其是接触数据结构时,会发现递归的出现频率非常之高,也行之有效~下面是笔者在接触递归这个东西时的一些个人总结和体会: 1.直接或间接 ... 
- django-admin和manage.py
			目录 一.Django内置命令选项 check dbshell diffsettings flush makemigrations migrate runserver shell startapp s ... 
- Codeforces Round #542 Div. 1
			A:显然对于起点相同的糖果,应该按终点距离从大到小运.排个序对每个起点取max即可.读题花了一年还wa一发,自闭了. #include<iostream> #include<cstd ... 
