当前位置:网站首页>Heap parsing and heap sorting
Heap parsing and heap sorting
2022-07-26 06:55:00 【Listen to the sea】
Pile up
The basic structure
The heap is a complete binary tree
Except for the last floor, it is full
The nodes of the last layer are in order from left to right
example :
Small cap pile : The value of each node is less than ( be equal to ) Values of left and right child nodes
Here is a small top pile

Storage
Use an array to store . Subscript from 1 Start , The root node is 1. If a node is x, Then the left son is 2x, The right son is 2x+1( Subscript from 0 At the beginning 2x+1,2x+2)
operation
( Take the small root pile for example )
Two basic operations
down sinking :
For example, after modifying the value of the root node , It needs to be readjusted , Then you need to compare the size of this point with the value of the two child nodes , Choose the smaller one to exchange with , Take this point down again and continue to compare , And so on
up rising :
Same thing : For example, modify the value of the last node , I need to compare it with its parent node , If it is less than the value of the parent node , In exchange for . And so on
Time complexity analysis of two basic operations : Both operations are proportional to the height of the tree , So all for logn
Five operations : use down,up Operation to achieve
- Insert a number . Insert... At the end , Conduct up operation
- For the minimum . Directly the root node
- Delete minimum . Delete root node , Put the last node at the root node ,down operation
- Delete any element . Put the last node in this position ,down,up( There are three situations , Immobility 、 Go up or down , although down,up With the , But there is only one case , Only one )
- Modify any element , In the same way ,down,up
One heap Array ,size Indicates the current heap size .
Heap sort
Enter a length of n The whole number sequence of , From small to large, before output m Small number .
Input format
The first line contains integers n and m.
The second line contains n It's an integer , Represents an integer sequence .
Output format
All in one line , contain m It's an integer , Represents the first in an integer sequence m Small number .
Data range
1≤m≤n≤1e5,
1≤ The elements in the sequence ≤1e9
sample input :
5 3
4 5 1 3 2
sample output :
1 2 3
Ideas :
A property of complete binary tree is used , Subscript from 1 Start , Subscript <= n / 2 All nodes of are non leaf nodes , Greater than n / 2 All are leaf nodes , We start with non leaf nodes and move forward one by one down operation . The heap top is the smallest value at present , The value of taking out the top of the heap every time is the minimum value , And carry out the above 5 Of the operations 3 Operations , That is, delete the minimum , Refactoring heap .C++ The code implementation is as follows
Code :
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int n, m, s;
int h[N];
void down(int u) {
int t = u;
if (2 * u <= s && h[2 * u] < h[t]) t = 2 * u;
if (2 * u + 1 <= s && h[2 * u + 1] < h[t]) t = 2 * u + 1;
if (u != t) swap(h[u], h[t]), down(t);
}
int main()
{
cin >> n >> m;
s = n;
for (int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);
for (int i = n / 2; i >= 1; i -- ) {
down(i);
}
while (m -- ) {
printf("%d ", h[1]);
h[1] = h[s -- ];
down(1);
}
return 0;
}
边栏推荐
- 2万字带你从0到1搭建一套企业级微服务安全框架
- How to solve the crash when the easygbs platform edits the device management group?
- Database performance test (MySQL)
- An album has been released, from introductory practical demonstration to advanced live Q & A, playing with container service so easy~
- Download, installation and development environment construction of "harmonyos" deveco
- Do you think you are a reliable test / development programmer? "Back to the pot"? Surface and reality
- 二叉树知识总结
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- 『牛客|每日一题』有效括号序列
- 『牛客|每日一题』 栈的压入、弹出序列
猜你喜欢

Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (multiple choice) June 2022

软考可以查成绩了,2022年上半年软考成绩查询入口已开通

曲线曲率展示

20000 words will take you from 0 to 1 to build an enterprise level microservice security framework

『HarmonyOS』DevEco的下载安装与开发环境搭建

Vim中删除^M

『HarmonyOS』探索HarmonyOS应用

快速排序(quick-sort)

mysql优化之show profile的使用及分析

常用的cmd指令
随机推荐
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
Can you learn fast and well with dual stream network? Harbin Institute of Technology & Microsoft proposed a distillation dual encoder model for visual language understanding, which can achieve fast an
buuReserve(4)
LeetCode刷题1:题目分类
Shared lock
[database] CTE (common table expression)
Downloadutilse tool class without error
Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (multiple choice) June 2022
<二> objectARX开发:创建和编辑基本图形对象
『牛客|每日一题』点击消除
[hardware ten treasures] - 7.1 [dynamic RAM] key points of DDR hardware design
[hard ten treasures] - 7.2 [dynamic RAM] analysis of the difference between DDR4 and DDR3
字符串和内存函数
mysql优化之索引及索引失效
替换license是否要重启数据库?
mySql建表的基本操作 与常见的函数
Merge_sort
少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(选择题)2022年6月
How to open an account online for Guohai Securities? Is it safe to open an account
Heap sort