标签:堆排序
#include <stdio.h> #include <stdlib.h> int * DuiPaixu(int a[],int n){ int end = n,i,t,x,y; while(end >= 1){ while(1){ int flag = 0; i=end/2; while(i > 0){ if(a[i] < a[2*i]){ t = a[i]; a[i] = a[2*i]; a[2*i] = t; flag = 1; } if(2*i+1 <= end&&a[i] < a[2*i+1]){ x = a[i]; a[i] = a[2*i+1]; a[2*i+1] = x; flag = 1; } i--; } if(!flag) break; } y = a[1]; a[1] = a[end]; a[end] = y; end--; } return a; } void Print(int a[],int n){ int i; for(i=1;i<=n;i++){ printf("%5d",a[i]); } } int main(void){ int n,i; int * a; printf("请输入数组长度n= "); scanf("%d",&n); a=(int*)malloc(n*sizeof(int)); printf("请输入数组元素: "); for(i=1;i<=n;i++){ scanf("%d",&a[i]); } a = DuiPaixu(a,n); Print(a,n); return 0; }标签:堆排序
原文地址:http://blog.51cto.com/13645380/2103079