当前位置:网站首页>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 ᖘ؏؏
边栏推荐
- Oracle memory management
- 35道MySQL面试必问题图解,这样也太好理解了吧
- Servlet
- SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
- Flutter动态化 | Fair 2.5.0 新版本特性
- Kotlin - improved decorator mode
- Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
- Red hat satellite 6: better management of servers and clouds
- 常见的几种最优化方法Matlab原理和深度分析
- Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
猜你喜欢

Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL

Today's sleep quality record 77 points

The principle of human voice transformer

Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)

Introduction to the implementation principle of rxjs observable filter operator

【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

Internet of things completion -- (stm32f407 connects to cloud platform detection data)
![[redis] cache warm-up, cache avalanche and cache breakdown](/img/df/81f38087704de36946b470f68e8004.jpg)
[redis] cache warm-up, cache avalanche and cache breakdown

物联网毕设 --(STM32f407连接云平台检测数据)
随机推荐
The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
Logback 日志框架
常见的几种最优化方法Matlab原理和深度分析
Flutter动态化 | Fair 2.5.0 新版本特性
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
服务器硬盘冷迁移后网卡无法启动问题
The principle of human voice transformer
Seven habits of highly effective people
[colab] [7 methods of using external data]
18W word Flink SQL God Road manual, born in the sky
Ubuntu 14.04 下开启PHP错误提示
Some thoughts on business
SSH login server sends a reminder
elk笔记24--用gohangout替代logstash消费日志
Today's sleep quality record 77 points
Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
父亲和篮球
Road construction issues
研发团队资源成本优化实践