堆排序的C语言实现

#include<stdio.h>//堆排序 
#define leftchild(i) (2*i+1)

void percdown(int a[],int i,int n){
	int child;
	int temp;
	for(temp=a[i];leftchild(i)<n;i=child){
		child=leftchild(i);
		if(child!=n-1&&a[child+1]>a[child])
		child++;
		if(temp<a[child])
		a[i]=a[child];
		else
		break;
	}
	a[i]=temp;
}
void swap(int *x,int *y){
	int t;
	t=*x;
	*x=*y;
	*y=t;
} 

void heapsort(int a[],int n){
	int i;
	for(i=n/2;i>=0;i--)
	percdown(a,i,n);//有这个数组建立一个堆
	for(i=n-1;i>0;i--){
		swap(&a[0],&a[i]);
		percdown(a,0,i);//交换最后一个数与第一个数,然后对剩下的数进行堆排序 
	}
}

int main(){
	int n;
	scanf("%d",&n);
	int i;
	int a[n];
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	heapsort(a,n);
	for(i=0;i<n;i++)
	printf("%d ",a[i]);
	return 0;
} 

堆排序的具体实现步骤

#include<stdio.h>//堆排序,输出了每一步的排序 
#define leftchild(i) (2*i+1)

void percdown(int a[],int i,int n){//用数组a[ ]下滤建立一个堆 
	int child;
	int temp;
	for(temp=a[i];leftchild(i)<n;i=child){//孩子要小于总结点 
		child=leftchild(i);
		if(child!=n-1&&a[child+1]>a[child])
		child++;
		if(temp<a[child])
		a[i]=a[child];
		else
		break;
	}
	a[i]=temp;
}

void swap(int *x,int *y){//swap函数,交换两个数 
	int t;
	t=*x;
	*x=*y;
	*y=t;
} 

void heapsort(int a[],int n){
	int i,j;
	for(i=n/2;i>=0;i--)//从n/2开始,是因为从最大的父节点开始比较 
	percdown(a,i,n);//有这个数组建立一个堆
	
	for(i=n-1;i>0;i--){
		swap(&a[0],&a[i]);
		percdown(a,0,i);//交换最后一个数与第一个数,然后对剩下的数进行堆排序 
		
		for(j=0;j<n;j++){//验证每一步排序 
		printf("%d ",a[j]);
		}
		printf("\n");
	}
}

int main(){
	int n;
	scanf("%d",&n);
	int i;
	int a[n];
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	heapsort(a,n);
	return 0;
} 

一个简单的例子

堆排序不是稳定的排序,由下面这个例子可知a【3】最后变为了a【6】(下面图上写错了,应该是a【6】)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值