#include#define N 20 int main() { void heapSort(int *,int); int i,a[N+1]; printf("Enter twenty numbers:\n"); for(i=1;i<=N;i++)/*由于堆排序用得是完全二叉树,所以元素的存储必须从1开始*/ scanf("%d",a+i); heapSort(a,N); for(i=1;i<=N;i++) printf("%d ",a[i]); return 0; } /*本函数完成对在数组a[low]到a[high]范围内对在位置low上的节点进行调整*/ void Sift(int *a,int low,int high) { int i=low,j=i*2;/*a[j]是a[i]的左孩子结点*/ int temp=a[i];/*保存位置low上的结点*/ while(j<=high) { if(j =1;--i)/*建立初始堆*/ Sift(a,i,n); for(i=n;i>=2;--i)/*进行n-1次循环完成堆排序*/ { temp=a[1];/*以下3句换出了根节点中的元素,将其放入最终位置*/ a[1]=a[i]; a[i]=temp; Sift(a,1,i-1);/*由于这次只需要调整根节点,而且元素个数减少了1个*/ } }