c语言堆排序,C语言实现堆排序代码

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

void heapsort(int arr[],unsigned int N)

{

unsigned int n = N,i = n/2,parent,child;

int t;

for (;;) { /* Loops until arr is sorted */

if (i > 0) { /* First stage - Sorting the heap */

i--; /* Save its index to i */

t = arr[i]; /* Save parent value to t */

} else { /* Second stage - Extracting elements in-place */

n--; /* Make the new heap smaller */

if (n == 0) return; /* When the heap is empty,we are done */

t = arr[n]; /* Save last value (it will be overwritten) */

arr[n] = arr[0]; /* Save largest value at the end of arr */

}

parent = i; /* We will start pushing down t from parent */

child = i*2 + 1; /* parent's left child */

/* Sift operation - pushing the value of t down the heap */

while (child < n) {

if (child + 1 < n && arr[child + 1] > arr[child]) {

child++; /* Choose the largest child */

}

if (arr[child] > t) { /* If any child is bigger than the parent */

arr[parent] = arr[child]; /* Move the largest child up */

parent = child; /* Move parent pointer to this child */

//child = parent*2-1; /* Find the next child */

child = parent*2+1; /* the prevIoUs line is wrong*/

} else {

break; /* t's place is found */

}

}

arr[parent] = t; /* We save t in the heap */

}

}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

总结

以上是编程之家为你收集整理的C语言实现堆排序代码全部内容,希望文章能够帮你解决C语言实现堆排序代码所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值