当前位置:网站首页>Detailed explanation of heap sort code
Detailed explanation of heap sort code
2022-07-04 22:38:00 【@Landscape post yuan】
Figure displaying the heap
• The heap is a complete binary tree ;
• The value of each node in the heap must be greater than or equal to ( Or less than or equal to ) Of each node in its subtree value .
The left side is the big top pile The right side is a small top pile
The following code establishes the big top heap
#include "stdio.h"
# define MAXSIZE 10
typedef struct {
int r[MAXSIZE]; /* use On save Store want row order Count Group */
int length ; /* use On remember record along order surface Of Long degree */
}SqList ;
void swap ( SqList *L, int i, int j){ /* hand over in L in Count Group r Of Next mark by i and j Of value */
int temp = L->r[i];
L->r[i] = L->r[j];
L->r[j] = temp;
}
void HeapAdjust(SqList* L,int s,int m){ /* Structure L The location of , The location of the first judgment node s, The number of nodes m */
int temp , j;
temp = L->r[s]; /* Pass to temp Location s Value on */
for (j = 2*s;j <= m;j *= 2) { /* Compare positions s And its two children */
if (j < m && L->r[j] < L->r[j+1]) /* First find s My two children , Find out the larger value of children */
++j; /* j The following table shows older children */
if ( temp >= L->r[j]) /* If temp Big be The current structure remains unchanged */
break ; /* r[c] Should be insert Enter into stay position Set up s On */
L->r[s] = L->r[j]; /* Otherwise, assign the maximum value to s position */
s = j; /* by L->r[s] = temp Do matting */
}
L->r[s] = temp; /* The last step */
}
void HeapSort(SqList* L){ /* For the sequence table L Make a heap sort */
int i;
for (i = L->length/2; i > 0; i--) /* hold L in Of r structure build become One individual Big The top Pile up */
HeapAdjust (L,i,L->length) ; /* Afferent structure L The location of The location of the first judgment node i The number of nodes length */
for(i=0;i<MAXSIZE;i++){ /* View the result after heap */
printf("%d ",L->r[i]);
}
printf("\n");
for (i = L->length; i > 1; i--) {
swap (L, 1 , i) ; /* Put the maximum value at the top of the heap at the end of the array ,i-1 Then he won't participate in the heap process */
HeapAdjust (L, 1 , i-1); /* Structure L The location of , Location of the first node s, The number of nodes m */
for(int j=0;j<MAXSIZE;j++){ /* Check out the process */
printf("%d--",L->r[j]);
}
printf("\n");
}
}
int main(){
int i;
int a[9]={50,10,90,30,70,40,80,60,20}; /* initialization */
SqList L;
L.length=MAXSIZE-1; /* The key : r[0] No need and The effective length needs to be reduced from the base 1 */
L.r[0]=9999999;
for(i=1;i<MAXSIZE;i++){
L.r[i]=a[i-1]; /* r[0] No need */
}
for(i=0;i<MAXSIZE;i++){
printf("%d ",L.r[i]); /* Check the original sorting */
}
printf("\n");
HeapSort(&L); /* Heap up */
for(i=0;i<MAXSIZE;i++){ /* View the sorted results */
printf("%d ",L.r[i]);
}
return 0;
}
边栏推荐
- Common open source codeless testing tools
- 达梦数据凭什么被称为国产数据库“第一股”?
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- php短视频源码,点赞时会有大拇指动画飘起
- Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
- Logo special training camp Section IV importance of font design
- Lost in the lock world of MySQL
- LOGO特训营 第二节 文字与图形的搭配关系
- LOGO特訓營 第三節 首字母創意手法
- Logo special training camp section 1 Identification logo and logo design ideas
猜你喜欢
Why is Dameng data called the "first share" of domestic databases?
Wake up day, how do I step by step towards the road of software testing
Logo special training camp Section V font structure and common design techniques
安装人大金仓数据库
集群的概述与定义,一看就会
蓝队攻防演练中的三段作战
将QA引入软件开发生命周期是工程师要遵循的最佳实践
It is said that software testing is very simple, but why are there so many dissuasions?
UML图记忆技巧
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
随机推荐
Huawei Nova 10 series released Huawei application market to build a solid application security firewall
【OpenGL】笔记二十九、抗锯齿(MSAA)
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
攻防世界 MISC 进阶区 Ditf
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Deployment of JVM sandbox repeater
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
The proofreading activity of data science on the command line second edition was restarted
微服务--开篇
繁華落盡、物是人非:個人站長該何去何從
攻防世界 misc 高手进阶区 a_good_idea
Scala download and configuration
高中物理:直线运动
将QA引入软件开发生命周期是工程师要遵循的最佳实践
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
Common open source codeless testing tools
LOGO特訓營 第一節 鑒別Logo與Logo設計思路
Practice and principle of PostgreSQL join