当前位置:网站首页>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;
}
边栏推荐
- Differences and relations between varchar and nvarchar in database
- Leetcode question brushing 1: topic classification
- MySQL read / write lock
- What to pay attention to when using German chicks for the first time
- Intention lock
- SQL shell (PSQL) tool under PostgreSQL
- 【数据库】CTE(Common Table Expression(公共表表达式))
- one hundred and twenty-three million one hundred and twenty-three thousand one hundred and twenty-three
- vulnhub Lampião: 1
- Resume considerations
猜你喜欢

Experimental flags: --disable_ admission_ control=false --enable_ rm=false --llama_ callback_ port=28000

Address resolution ARP Protocol

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

Press in and pop-up sequence of "Niuke | daily question" stack

vulnhub Lampião: 1

Queue assistant | product update log in June 2022

XSS labs (1-10) break through details

『牛客|每日一题』有效括号序列

Delete ^m from VIM

vulnhub Lampião: 1
随机推荐
微信小程序 - 从入门到入土
MySQL intent lock
软考可以查成绩了,2022年上半年软考成绩查询入口已开通
MySQL optimized index and index invalidation
<二> objectARX开发:创建和编辑基本图形对象
在第一次使用德国小鸡要注意的地方
Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (multiple choice) June 2022
What to pay attention to when using German chicks for the first time
归并排序(merge_sort)
「“xxxx“正在运行,可能导致系统卡顿,降低待机时间,点按关闭」处理
Proxyman, a native high-performance packet capturing tool, is for you who love learning
曲线曲率展示
vulnhub Lampião: 1
III Actual combat - current time representation and world standard time format
Vim中删除^M
Quick sort
mySql建表的基本操作 与常见的函数
日志轮转logrotate
2万字带你从0到1搭建一套企业级微服务安全框架
Celery takes up large memory - memory leak