当前位置:网站首页>堆排序(原理加代码)
堆排序(原理加代码)
2022-06-22 18:28:00 【单单一个越字】
博客园他人教程:
https://www.cnblogs.com/chengxiao/p/6129630.html
C语言代码:
#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);//从下往上,从左往右,将初始的无序序列构造大顶堆
}
for( i=n; i > 1; i-- )
{
swap(k, 1, i);//将堆顶和最后一个节点交换
HeapAdjust(k, 1, i-1);//从上往下(已知根节点已发生变化),从左往右构造大顶堆
}
}
int main()
{
int i, a[10] = {
-1, 5, 2, 6, 0, 3, 9, 1, 7, 4};//a[0]当作空位,根节点从a[1]开始,更容易分析代码
HeapSort(a, 9);
printf("排序后的结果是:");
for( i=1; i < 10; i++ )
{
printf("%d", a[i]);
}
printf("\n\n");
return 0;
}
边栏推荐
- Fault analysis | from data_ Free exception
- Openpnp调试 ------ 0816飞达推0402编带
- Take the file name in the zip package
- Service practice: use service to complete a download task
- NLP-D57-nlp比赛D26&刷题D13&读论文&&找了一个多小时bug
- Shell script explanation (VII) -- regular expression, sort, uniq, tr
- Adapter mode of structural mode
- 修改隐含参数造成SQL性能下降案例之二
- Download files through Base64 (convert Base64 to BLOB)
- Velocity 语法
猜你喜欢

Pull down refresh and pull up to load more listviews

About Random Forest

Flutter series - build a flutter development environment

0816飞达的缺点(改进方向)

Interface development component devaxpress asp Net core v21.2 - UI component enhancements
![K个一组翻转链表[链表拆解/翻转/拼装]](/img/70/fb783172fa65763f031e6bd945cbd9.png)
K个一组翻转链表[链表拆解/翻转/拼装]

ActiveReports报表实战应用教程(十九)——多数据源绑定

Some problem records of openpnp using process

NAND闪存(NAND Flash)颗粒SLC,MLC,TLC,QLC的对比

Modify the antd tree component so that its subclasses are arranged horizontally.
随机推荐
Altium Designer中off grid pin解决方法
Array objects can be compared one by one (the original data with the same index and ID will be retained, and the data not in the original array will be added from the default list)
Pat a 1093 count Pat's (25 points)
如何用银灿IS903主控DIY自己的U盘?(练习BGA焊接的好项目)
Experiment 7 trigger
Pull down refresh and pull up to load more listviews
创建者模式大汇总
Do you use thread or service?
Fault analysis | from data_ Free exception
Redis 大 key 问题
生产系统SQL执行计划突然变差怎么办?
AttributeError: ‘KeyedVectors‘ object has no attribute ‘wv‘
A homekit enabled camera? Zhiting IPC camera IC1 unpacking experience
Shell script explanation (IV) -- while loop and until loop of loop statements (additional examples and analysis)
Common technical notes
0.0 - how can SolidWorks be uninstalled cleanly?
Intelligent procurement system solution for processing and manufacturing industry: help enterprises realize integrated and Collaborative Procurement in the whole process
calendar控件编程
实验七 触发器
Chapter I 100 hot questions (1-5)