堆排序

#include

/* run this program using the console pauser or add your own getch, system(“pause”) or input loop /
void heap_adjust(int a,int f,int n)
{
int c=2
f+1;
int t=a[f];
while(c<n){
if(c+1<n&&a[c+1]>a[c])c++;
if(a[f]>=a[c])break;
a[f]=a[c];
a[c]=t;
f=c;
c=2
f+1;
}
}
void heap_build(int *a,int n)
{
for(int i=(n-1)/2;i>=0;i–){
heap_adjust(a,i,n);
}
}
void heap_sort(int *a,int beg,int end)
{
heap_build(a+beg,end-beg);
for(int t,i=end-beg-1;i>=1;i–){
t=(a+beg)[0];(a+beg)[0]=(a+beg)[i];(a+beg)[i]=t;
heap_adjust(a+beg,0,i);

}
}
int main(int argc, char** argv) {
int a[10];
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
heap_sort(a,0,n);
for(int i=0;i<n;i++)printf("%d",a[i]);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值