void AdjustHeap(int *a, int size,int root)//建最大堆
创新互联公司一直通过网站建设和网站营销帮助企业获得更多客户资源。 以"深度挖掘,量身打造,注重实效"的一站式服务,以成都网站设计、网站制作、移动互联产品、成都全网营销推广服务为核心业务。10年网站制作的经验,使用新网站建设技术,全新开发出的标准网站,不但价格便宜而且实用、灵活,特别适合中小公司网站制作。网站管理系统简单易用,维护方便,您可以完全操作网站资料,是中小公司快速网站建设的选择。
{
if (a == NULL )
{
return;
}
int child = root*2+1;
while (child<size)
{
if ((child + 1) < size && a[child] < a[child + 1])
{
++child;
}
if (a[root]<a[child])
{
swap(a[child], a[root]);
}
root = child;
child = root * 2 + 1;
}
}
void Adjustdown(int *a, int size, int root)//向下调整,将堆顶的数据换到堆底后把长度减一;
{ //再将堆顶数据向下比较,建成最大堆
int child = root * 2 + 1;
while (child < size)
{
if ((child + 1) < size && a[child] < a[child + 1])
{
++child;
}
if (a[child] > a[root])
{
swap(a[child], a[root]);
}
root = child;
child = root * 2 + 1;
}
}
void HeapSort(int *a,int length)
{
if (a == NULL || length <= 0)
{
return;
}
for (int i = (length - 2) / 2; i >= 0; i--)
{
AdjustHeap(a, length, i);
}
for (int i = length - 1; i > 0; i--)
{
int tmp = a[0];
a[0] = a[i];
a[i] = tmp;
Adjustdown(a, i, 0);
}
}
网页名称:堆排序的基本实现
本文来源:https://www.cdcxhl.com/article46/gihhhg.html
成都网站建设公司_创新互联,为您提供营销型网站建设、微信小程序、静态网站、外贸建站、云服务器、虚拟主机
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联