当前位置:网站首页>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;
}
边栏推荐
- Energy momentum: how to achieve carbon neutralization in the power industry?
- PostgreSQL服务端编程聚合和分组
- TLA+ 入门教程(1):形式化方法简介
- 关于栈区、堆区、全局区、文字常量区、程序代码区
- Concurrent network modular reading notes transfer
- How to manage 15million employees easily?
- Jvm-Sandbox-Repeater的部署
- UML diagram memory skills
- 串口数据帧
- La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
猜你喜欢
![[Yugong series] go teaching course 003-ide installation and basic use in July 2022](/img/65/b36ca239e06a67c01d1eb0b4648f71.png)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022

共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf

Locust performance test - environment construction and use

Nat. Commun.| Machine learning jointly optimizes the affinity and specificity of mutagenic therapeutic antibodies

串口数据帧

SPSS installation and activation tutorial (including network disk link)

Energy momentum: how to achieve carbon neutralization in the power industry?

Unity-VScode-Emmylua配置报错解决

质量体系建设之路的分分合合

10 schemes to ensure interface data security
随机推荐
With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
Detailed explanation of flask context
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
TLA+ 入门教程(1):形式化方法简介
啃下大骨头——排序(二)
Play with grpc - go deep into concepts and principles
PostgreSQL JOIN实践及原理
Éducation à la transmission du savoir | Comment passer à un test logiciel pour l'un des postes les mieux rémunérés sur Internet? (joindre la Feuille de route pour l'apprentissage des tests logiciels)
业务太忙,真的是没时间搞自动化理由吗?
华泰证券是国家认可的券商吗?开户安不安全?
好用app推荐:扫描二维码、扫描条形码并查看历史
攻防世界 MISC 进阶区 hit-the-core
BigFilter全局交易防重组件的介绍与应用
Tiktok actual combat ~ the number of comments is updated synchronously
如何实现轻松管理1500万员工?
Easy to use app recommendation: scan QR code, scan barcode and view history
Mongodb aggregation operation summary
国产数据库乱象
md5工具类
10 schemes to ensure interface data security
