本文共 1077 字,大约阅读时间需要 3 分钟。
//堆排序的原理是遍历数组中的值,得到第一个值为最小,与最后一个值对换,再次遍历,第一个值与倒数第二个值替换,以此类推!#include#include #define MAX 255int R[MAX];void Heapify(int s,int m){ /*对R[1..n]进行堆调整,用temp做暂存单元 */ int j,temp; temp=R[s]; j=2*s; while (j<=m) { if (R[j]>R[j+1]&&j 0;i--) Heapify(i,n);}void Heap_Sort(int n){ /* 对R[1..n]进行堆排序,不妨用R[0]做暂存单元 */ int i; BuildHeap(n); /* 将R[1-n]建成初始堆 */ for(i=n;i>1;i--) { /* 对当前无序区R[1..i]进行堆排序,共做n-1趟。 */ R[0]=R[1]; R[1]=R[i];R[i]=R[0]; /* 将堆顶和堆中最后一个记录交换 */ Heapify(1,i-1); /* 将R[1..i-1]重新调整为堆,仅有R[1]可能违反堆性质 */ } /* end of for */} /* end of Heap_Sort */void main(){ int i,n; //clrscr(); puts("Please input total element number of the sequence:"); scanf("%d",&n); if(n<=0||n>MAX) { printf("n must more than 0 and less than %d.\n",MAX); exit(0); } puts("Please input the elements one by one:"); for(i=1;i<=n;i++) scanf("%d",&R[i]); puts("The sequence you input is:"); for(i=1;i<=n;i++) printf("%4d",R[i]); Heap_Sort(n); puts("\nThe sequence after Big heap_sort is:"); for(i=1;i<=n;i++) printf("%4d",R[i]); puts("\n Press any key to quit..."); getchar();}
转载地址:http://fytmi.baihongyu.com/