当前位置:网站首页>2. Heap sort "hard to understand sort"
2. Heap sort "hard to understand sort"
2022-07-07 15:15:00 【Mu Quanyu [dark cat]】
Heap sort (Heapsort) It is a sort algorithm designed by using the data structure of heap . This heap is an approximately complete binary tree structure , And it's also a big pile , Sorting is also based on this big top heap .
Why do you say Heap sort It's hard to understand ? answer : The main reason is some people Maybe not Binary tree , I don't know what is Pile up , Never learned The idea of simulating arrays . in other words He doesn't have these In the case of premise knowledge points , How is that possible? Good understanding Heap sorting . What's more? Heap sort Adjust the implementation of the heap , also Yes Our biggest recursion . and Overall sorting idea , once lecturer I didn't make it clear , Probably Just Is a little knowledge .
1.1 Algorithm description
- Use array to simulate binary tree structure , Make the left and right nodes of each root node The corresponding subscript is
left = 2k + 1
、right = 2k + 2
. - Put this binary tree Initialize to Big pile top , Here be used Recursive operation . Thought is Start with the last subtree , forward Put each subtree All become big piles ! Of course there will be Aftereffect problem (
namely When we After adjusting the node order , The node being exchanged The subtree that is the root node Is it still Big top pile ??
), therefore We here Need recursive , then Go to Handle All subtrees under this node . - because Root node of the entire binary tree yes Maximum value , So we Sure hold This node and Last A node swapping . namely stay 1 ~ n Within the range of nodes , This The root node yes Maximum , We will take it. Put it in Last . then Let's put 1 ~ n – 1 A binary tree with a range of nodes ( exclude The last node ) Let it become Big pile top , then Go again Conduct The above operations , Put the penultimate Maximum Put it in The penultimate position . And so on , When we Only The first node of the binary tree , Then the whole Orderly !!
1.2 Dynamic diagram demonstration
1.3 Code implementation
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
vector<int> arr = {
91,60,96,13,35,65,46,65,10,30,20,31,77,81,22};
int len;
void swapF(int k){
// To k by The root node The subtree of Conduct Explore and adjustment
int left = 2 * k + 1;
int right = 2 * k + 2;
int father = k;// To be updated The root node , Probably The root node need Adjust and update
if(left < len && arr[left] > arr[father]){
father = left;// The left node Than The root node Big , therefore The subscript of the left node Save to
// father in , advance get set Location of the root node
}
if(right < len && arr[right] > arr[father]){
father = right;// if The largest node yes Right node , that father = right
}
if(father != k){
// If The subtree is adjustment , namely The root node change
// then Handle Aftereffect problem
swap(arr[father],arr[k]);// First the Adjust this subtree
swapF(father);// then Come on look down The location of the node we exchanged The data has changed
// After change , whether also keep Piled on top nature
}
}
void init(){
len = arr.size();
for(int i = floor(len / 2);i >= 0; i--){
// from the last one Subtree start
swapF(i);
}
}
void heapSort(){
init();
for(int i = len - 1;i > 0;--i){
// Conduct Sort , Take every one. Within the interval Maximum value , all Put it in Where to put it
swap(arr[i],arr[0]);
len--;
swapF(0);
}
}
int main(void){
heapSort();
for(auto x : arr){
cout << x << " ";
}
return 0;
}
Heap sort In two parts : Adjustment of pile + The order of the heap .
Time complexity :O(n) + O(nlogn) = O(nlogn)
Spatial complexity :O(1)
边栏推荐
- 【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
- [understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper
- Niuke real problem programming - Day9
- MongoD管理数据库的方法介绍
- Niuke real problem programming - day18
- “百度杯”CTF比赛 2017 二月场,Web:include
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
- 居然从408改考自命题!211华北电力大学(北京)
- PAT 甲级 1103 Integer Factorizatio
- 15、文本编辑工具VIM使用
猜你喜欢
CPU与chiplet技术杂谈
微信小程序 01
暑期安全很重要!应急安全教育走进幼儿园
Unity's ASE achieves full screen sand blowing effect
Promoted to P8 successfully in the first half of the year, and bought a villa!
[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper
Bye, Dachang! I'm going to the factory today
Niuke real problem programming - day18
知否|两大风控最重要指标与客群好坏的关系分析
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
随机推荐
CTFshow,信息搜集:web4
Niuke real problem programming - day14
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
Niuke real problem programming - day20
15、文本编辑工具VIM使用
Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
Stm32f103c8t6 PWM drive steering gear (sg90)
CTFshow,信息搜集:web5
Used by Jetson AgX Orin canfd
【目标检测】YOLOv5跑通VOC2007数据集
Ctfshow, information collection: web5
Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
Jetson AGX Orin CANFD 使用
What are PV and UV? pv、uv
Notes HCIA
“百度杯”CTF比赛 2017 二月场,Web:include
Bye, Dachang! I'm going to the factory today
Read PG in data warehouse in one article_ stat