当前位置:网站首页>Heap sort (principle plus code)
Heap sort (principle plus code)
2022-06-22 20:02:00 【Just one word】
Blog Garden others tutorial :
https://www.cnblogs.com/chengxiao/p/6129630.html
C The language code :
#include <stdio.h>
int count = 0;
void swap(int k[], int i, int j)
{
int temp;
temp = k[i];
k[i] = k[j];
k[j] = temp;
}
void HeapAdjust(int k[], int s, int n)
{
int i, temp;
temp = k[s];
for( i=2*s; i <= n; i*=2 )
{
if( i < n && k[i] < k[i+1] )
{
i++;
}
if( temp >= k[i] )
{
break;
}
k[s] = k[i];
s = i;
}
k[s] = temp;
}
void HeapSort(int k[], int n)
{
int i;
for( i=n/2; i > 0; i-- )
{
HeapAdjust(k, i, n);// From bottom to top , From left to right , Construct a large top heap from the initial unordered sequence
}
for( i=n; i > 1; i-- )
{
swap(k, 1, i);// Swap the top of the heap with the last node
HeapAdjust(k, 1, i-1);// From the top down ( The root node is known to have changed ), Construct a large top pile from left to right
}
}
int main()
{
int i, a[10] = {
-1, 5, 2, 6, 0, 3, 9, 1, 7, 4};//a[0] As a vacancy , The root node is from a[1] Start , Easier to analyze code
HeapSort(a, 9);
printf(" The result of sorting is :");
for( i=1; i < 10; i++ )
{
printf("%d", a[i]);
}
printf("\n\n");
return 0;
}
边栏推荐
- 北京大学|通过对比学习实现离线元强化学习的鲁棒任务表示
- [compréhension approfondie de la technologie tcaplusdb] exploitation et entretien de tcaplusdb - inspection quotidienne des patrouilles
- 安装Office的一些工具
- 【深入理解TcaplusDB技术】集群管理操作
- 修改隐含参数造成SQL性能下降案例之一
- c# winform 嵌入flash
- Recommend an anatomy website
- Huffman tree (C language)
- Fault analysis | from data_ Free exception
- [petty bourgeoisie database] break down the concept: data, database, database system, database management system, database technology
猜你喜欢

How to use yincan IS903 to master DIY's own USB flash disk? (good items for practicing BGA welding)

Nrf51822 peripheral learning

2. what is mechanical design?

Focal and global knowledge distillation for detectors

元宇宙怎么就这么火,市场喊起来的10万亿是吹嘘还是真相?

Chapter I 100 hot questions (1-5)

Nlp-d57-nlp competition D26 & skimming questions D13 & reading papers & finding bugs for more than an hour

Solution of off grid pin in Altium Designer

Follow up course supplement of little turtle teacher "take you to learn C and take you to fly"

Damp 3D printer consumables
随机推荐
Peking University - robust task representation for off-line meta reinforcement learning through contrastive learning
matlab调用API
c# winform 嵌入flash
MySQL数据库DQL练习题
MySQL多表操作练习题
B tree code (C language)
Redis 大 key 问题
matplotlib设置坐标轴刻度间隔
【小资说库】掰扯下概念:数据、数据库、数据库系统、数据库管理系统、数据库技术
lua--数据类型、变量、循环、函数、运算符的使用
谷歌| ICML 2022: 深度强化学习中的稀疏训练状态
关键路径
Assign values to objects
University of Calgary | recommendation system based on Reinforcement Learning
Velocity 语法
0.1----- process of drawing PCB with AD
【深入理解TcaplusDB技术】TcaplusDB运维单据
A text to show you the memory leak
510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming
漫话Redis源码之一百一十九