当前位置:网站首页>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 ᖘ؏؏
边栏推荐
- Anan's doubts
- MapReduce实现矩阵乘法–实现代码
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 7 exercises]
- [how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
- 今日睡眠质量记录77分
- Resolved (error in viewing data information in machine learning) attributeerror: target_ names
- MapReduce implements matrix multiplication - implementation code
- PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
- 网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
- This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
猜你喜欢

显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti

MySQL

Flink SQL knows why (19): the transformation between table and datastream (with source code)
![[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]](/img/b4/3442c62586306b4fceca992ce6294a.png)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]

elk笔记24--用gohangout替代logstash消费日志

JSP and filter

8皇后问题

已解决(机器学习中查看数据信息报错)AttributeError: target_names

Servlet

Seven habits of highly effective people
随机推荐
8皇后问题
Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
Logback log framework
MapReduce implements matrix multiplication - implementation code
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
106. How to improve the readability of SAP ui5 application routing URL
已解决(机器学习中查看数据信息报错)AttributeError: target_names
Oracle memory management
rxjs Observable filter Operator 的实现原理介绍
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
The principle of human voice transformer
Swiftui development experience: the five most powerful principles that a programmer needs to master
35道MySQL面试必问题图解,这样也太好理解了吧
Mysql database basic operation - regular expression
阿南的疑惑
Open PHP error prompt under Ubuntu 14.04
Servlet