标签:
理解理解理解万岁!!!
#include<cstdio> #include<cstring> #include<cstdlib> #define MAXSIZE 10 typedef struct { int r[MAXSIZE+1]; int length; }SqList; void Swap(SqList *L,int i,int j){ int temp = L->r[i]; L->r[i] = L->r[j]; L->r[j] = temp; } void Assignment(SqList *L,int len,int *a){ int i; L->length = len; for(i=1;i<=len;i++){ L->r[i] = a[i-1]; } return ; } void Output(SqList L){ int i; for(i=1;i<=L.length;i++){ printf("%d ",L.r[i]); } printf("\n"); return; } //尽量自己跑一遍代码 //看不懂的详见《大话数据结构》399页,一定要全程跑一遍就明白大致意思了, void HeapAdjust(SqList *L,int s,int m){ //使得L中从s到m 构成大根堆 int temp,j; temp = L->r[s]; for(j=2*s;j<=m;j*=2){ //沿关键字较大的孩子结点向下筛选 if(j<m && L->r[j]<L->r[j+1]) ++j; //j为关键字中较大的记录的下标 if(temp>=L->r[j]) break; L->r[s] = L->r[j]; s = j; } L->r[s] = temp; return; } void HeapSort(SqList *L){ int i; for(i=L->length/2;i>0;i--){ HeapAdjust(L,i,L->length); } for(i=L->length;i>1;i--){ Swap(L,1,i); HeapAdjust(L,1,i-1); } return ; } int main(){ int a[10] = {9,1,5,8,3,7,4,6,2,10}; SqList L; Assignment(&L,10,a); HeapSort(&L); Output(L); return 0; }
标签:
原文地址:http://www.cnblogs.com/zhuozhuo/p/5450296.html