12.12学习总结

1.今日安排

堆排序

题目要求:本题要求实现堆排序中的筛选函数,待排序列的长度1<=n<=1000。

#include<stdio.h>
#include<stdlib.h>
typedef  int  KeyType;
typedef  struct {                      
  KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/                       
  int Length;      
}SqList;
typedef SqList HeapType; 
void  CreatSqListHeapType *L);/*待排序列建立,由裁判实现,细节不表*/ 
void HeapAdjust( HeapType  H, int s, int m);
void HeapSort( HeapType  H);
int main()
{
  HeapType L;
  int i;
  CreatSqList(&L);
  HeapSort(L);
  for(i=1;i<=L.Length;i++)
   {        
     printf("%d ",L.elem[i]);
   }
  return 0;
}
void HeapSort( HeapType  H)
{ /*堆顺序表H进行堆排序*/
  int i; KeyType rc;
  /*建立初始堆*/
  for( i=H.Length/2;i>0; i--)
   {
      HeapAdjust(H, i, H.Length);
   }
  for(i=H.Length;i>1;i--)
   {
      rc=H.elem[1];
      H.elem[1]=H.elem[i]; 
      H.elem[i]=rc;
      HeapAdjust(H, 1, i-1); 
   }
 }
void HeapAdjust( HeapType  H, int s, int m){
    int now=s;
    if(s*2<=m&&H.elem[s*2]>H.elem[now]){
        now<<=1;
    }
    if(s*2+1<=m&&H.elem[s*2+1]>H.elem[now]){
        now=s*2+1;
    }
    if(now!=s){
        int t=H.elem[s];
        H.elem[s]=H.elem[now];
        H.elem[now]=t;
        HeapAdjust(H,now,m);
    }
}

 

posted @ 2021-12-12 22:11  今天又双叒叕在敲代码  阅读(48)  评论(0编辑  收藏  举报