当前位置:网站首页>Heap structure and heap sort heapify
Heap structure and heap sort heapify
2022-07-03 13:31:00 【MarquiS_ houzf】
Heap structure and heap sorting heapify
Recommended by friends , stay b Stand and watch the order of the lanterns on the first lunar month . I think it's good . Subtotal summary .
Pile up heap:
The structure of the heap is a complete binary tree :
From top to bottom , From left to right . The parent node value > Child node ;
heapify( Form a heap structure sort ): The parent node exchanges with the largest child node , Form a heap structure ; from h-1 layer ( Next to last )
Logical formulas represent :
int arr[] = {10,5,8,3…};
node :i=3;
P=(i-1)/2;
c1=2i+1;
c2=2i+2;
Code :
// Array swapping ;
void swap(int arr[],int i,int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
// From the top down heapify( Form a heap structure sort );
void heapify(int tree[],int n,int i){
if(i>=n){
return;
}
int c1=2*i+1;
int c2=2*i+2;
int max=i;
if(c1<n&&tree[c1]>tree[max]){
max=c1
}
if(c2<n&&tree[c2]>tree[max]){
max=c2
}
if(max!=i){
swap(tree,max,i);
heapify(tree,n,max);
}
}
// Build heap from bottom to top ;
void build_heap(){
int last_node=n-1;
int parent=(last_node-1)/2;
int i;
for(i=parent;i>=0;i--){
heapify(tree,n,i);
}
}
// From small to large ;
void heap_sort(int tree[], int n){
build_heap(tree,n);
int i;
for(i=n-1;i>=0;i--){
swap(tree,i,0);
heapify(tree,i,0);
}
}
int main(){
int tree[]={
4,10,3,5,1,2};
int n=6;
heapify(tree,n,0);
int i;
for(i=0;i<n;i++;){
System.out.print(tree[i]);
}
return 0;
}
Like the great God of the first month or friends who see here , Just give praise and attention .thx!
If it helps you , Please pay attention to , give the thumbs-up , Collection , Three even .
Your affirmation , It's my motivation . I wish the Chinese nation an early rejuvenation ! Thank you. .
؏؏ᖗ A kind of ◡ A kind of ᖘ؏؏
边栏推荐
- Libuv库 - 设计概述(中文版)
- JS 将伪数组转换成数组
- JS convert pseudo array to array
- Elk note 24 -- replace logstash consumption log with gohangout
- Logback log framework
- Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
- Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
- Will Huawei be the next one to fall
- elk笔记24--用gohangout替代logstash消费日志
- The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
猜你喜欢
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
elk笔记24--用gohangout替代logstash消费日志
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
Resource Cost Optimization Practice of R & D team
106. How to improve the readability of SAP ui5 application routing URL
[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈..."/>
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
已解决(机器学习中查看数据信息报错)AttributeError: target_names
Setting up remote links to MySQL on Linux
Elk note 24 -- replace logstash consumption log with gohangout
35道MySQL面试必问题图解,这样也太好理解了吧
随机推荐
35道MySQL面试必问题图解,这样也太好理解了吧
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
Father and basketball
106. How to improve the readability of SAP ui5 application routing URL
MySQL_ JDBC
Setting up remote links to MySQL on Linux
The difference between stratifiedkfold (classification) and kfold (regression)
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
Server coding bug
json序列化时案例总结
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
KEIL5出现中文字体乱码的解决方法
Open PHP error prompt under Ubuntu 14.04
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
The network card fails to start after the cold migration of the server hard disk
Logback 日志框架
The difference between session and cookie
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
File uploading and email sending
Mysql database basic operation - regular expression