当前位置:网站首页>堆排序代码详解
堆排序代码详解
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;
}
边栏推荐
- Wake up day, how do I step by step towards the road of software testing
- Locust性能测试 —— 环境搭建及使用
- Introduction and application of bigfilter global transaction anti duplication component
- Logo special training camp Section IV importance of font design
- leetcode 72. Edit distance edit distance (medium)
- 抖音实战~评论数量同步更新
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- PostgreSQLSQL高级技巧透视表
- Challenges faced by virtual human industry
- 【lua】int64的支持
猜你喜欢

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

国产数据库乱象

Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像

How to transfer to software testing, one of the high paying jobs in the Internet? (software testing learning roadmap attached)

NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse

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

都说软件测试很简单有手就行,但为何仍有这么多劝退的?

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

Business is too busy. Is there really no reason to have time for automation?

Common open source codeless testing tools
随机推荐
How to reset the password of MySQL root account
Shell script implements application service log warehousing MySQL
【OpenGL】笔记二十九、抗锯齿(MSAA)
SPSS installation and activation tutorial (including network disk link)
Deployment of JVM sandbox repeater
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
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
SQL中MAX与GREATEST的区别
Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
Force buckle_ Palindrome number
制作条形码的手机App推荐
Recommendation of mobile app for making barcode
leetcode 72. Edit Distance 编辑距离(中等)
É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)
Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
面试必备 LeetCode 链表算法题汇总,全程干货!
并发优化总结
sobel过滤器
Easy to use app recommendation: scan QR code, scan barcode and view history
Li Kou 98: verify binary search tree
