当前位置:网站首页>堆排序代码详解
堆排序代码详解
2022-07-04 22:17:00 【@风景邮递Yuan】
堆的图示
• 堆是一个完全二叉树;
• 堆中每一个节点的值都必须大于等于(或小于等于)其子树中每个节点的 值。
左侧为大顶堆 右侧为小顶堆
下列代码建立的是大顶堆
#include "stdio.h"
# define MAXSIZE 10
typedef struct {
int r[MAXSIZE]; /* 用 于 存 储 要 排 序 数 组 */
int length ; /* 用 于 记 录 顺 序 表 的 长 度 */
}SqList ;
void swap ( SqList *L, int i, int j){ /* 交 换 L 中 数 组 r 的 下 标 为 i 和 j 的 值 */
int temp = L->r[i];
L->r[i] = L->r[j];
L->r[j] = temp;
}
void HeapAdjust(SqList* L,int s,int m){ /* 结构体L的位置,第一个判断的结点的位置s,结点的个数m */
int temp , j;
temp = L->r[s]; /* 传递给temp位置s上的值 */
for (j = 2*s;j <= m;j *= 2) { /* 比较位置s上和它的两个子孩子上的值 */
if (j < m && L->r[j] < L->r[j+1]) /* 先找到s的两个子孩子,找出子孩子中较大的值 */
++j; /* j表示较大子孩子的下表 */
if ( temp >= L->r[j]) /* 如果temp大 则 当前结构不变 */
break ; /* r[c] 应 插 入 在 位 置 s 上 */
L->r[s] = L->r[j]; /* 否则把最大值赋到s位置上 */
s = j; /* 为 L->r[s] = temp 做铺垫 */
}
L->r[s] = temp; /* 最后一步 */
}
void HeapSort(SqList* L){ /* 对顺序表L进行堆排序 */
int i;
for (i = L->length/2; i > 0; i--) /* 把 L 中 的 r 构 建 成 一 个 大 顶 堆 */
HeapAdjust (L,i,L->length) ; /* 传入结构体L的位置 第一个判断的结点的位置i 结点的个数length */
for(i=0;i<MAXSIZE;i++){ /* 查看堆化后的结果 */
printf("%d ",L->r[i]);
}
printf("\n");
for (i = L->length; i > 1; i--) {
swap (L, 1 , i) ; /* 把堆顶最大值放在数组最后面,i-1 后就不参与堆化过程了 */
HeapAdjust (L, 1 , i-1); /* 结构体L的位置,第一个结点的位置s,结点的个数m */
for(int j=0;j<MAXSIZE;j++){ /* 查看过程 */
printf("%d--",L->r[j]);
}
printf("\n");
}
}
int main(){
int i;
int a[9]={50,10,90,30,70,40,80,60,20}; /* 初始化 */
SqList L;
L.length=MAXSIZE-1; /* 关键: r[0]是不用的 而且 有效长度需要在基础上减 1 */
L.r[0]=9999999;
for(i=1;i<MAXSIZE;i++){
L.r[i]=a[i-1]; /* r[0]是不用的 */
}
for(i=0;i<MAXSIZE;i++){
printf("%d ",L.r[i]); /* 查看原本的排序 */
}
printf("\n");
HeapSort(&L); /* 进行堆化 */
for(i=0;i<MAXSIZE;i++){ /* 查看排序后结果 */
printf("%d ",L.r[i]);
}
return 0;
}
边栏推荐
- Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
- Logo special training camp Section V font structure and common design techniques
- Energy momentum: how to achieve carbon neutralization in the power industry?
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- LOGO special training camp section I identification logo and Logo Design Ideas
- Play with grpc - go deep into concepts and principles
- Implementation rules for archiving assessment materials of robot related courses 2022 version
- 30余家机构联合发起数字藏品行业倡议,未来会如何前进?
- Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
- 嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
猜你喜欢

The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展

并发优化总结

凭借了这份 pdf,最终拿到了阿里,字节,百度等八家大厂 offer

Logo special training camp section 1 Identification logo and logo design ideas

Convolutional neural network model -- lenet network structure and code implementation

Challenges faced by virtual human industry

LOGO特訓營 第三節 首字母創意手法

Naacl-22 | introduce the setting of migration learning on the prompt based text generation task

30余家机构联合发起数字藏品行业倡议,未来会如何前进?

The use of complex numbers in number theory and geometry - Cao Zexian
随机推荐
Concurrent optimization summary
[cooking record] - stir fried 1000 pieces of green pepper
醒悟的日子,我是怎么一步一步走向软件测试的道路
sobel过滤器
Solana chain application crema was shut down due to hacker attacks
Logo Camp d'entraînement section 3 techniques créatives initiales
Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
Shell script implements application service log warehousing MySQL
Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient
如何实现轻松管理1500万员工?
蓝队攻防演练中的三段作战
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
PMO: compare the sample efficiency of 25 molecular optimization methods
Use blocconsumer to build responsive components and monitor status at the same time
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
Easy to use app recommendation: scan QR code, scan barcode and view history
传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
Implementation rules for archiving assessment materials of robot related courses 2022 version
Introducing QA into the software development lifecycle is the best practice that engineers should follow
ApacheCN 翻译、校对、笔记整理活动进度公告 2022.7
