当前位置:   article > 正文

排序算法之堆排序及其C语言代码实现

堆排序代码c语言

概述:堆排,实际上是一种选择排序,只不过采用了堆这种数据结构,利用这种数据结构,使得在每次查找最大元素时,直接选取堆顶元素,从而时间大大缩短,相对简单选择排序算法来说,比较高效。
堆排序算法可描述如下:
1.将待排元素依次存入数组,该数组即为一颗完全二叉树
2.从最后一个非叶节点开始递减,逐次将每个相应的二叉树调整成最大堆,最后,整个二叉树即为最大堆;
3.交换堆顶元素与堆尾元素,调整新的堆为最大堆,数组分为两部分:堆与已排序列,直至最后一个堆元素。
从上面可以看出,堆排序大致可以分为三个步骤:
1.存入数据(完全二叉树)
2.将前n个元素调整为最大堆
3.交换堆顶和堆尾元素,n=n-1,转2
注:本文元素从数组下标1开始储存,所以parent访问二叉树左右儿子分别为2*parent 和 2*parent+1;
两个核心函数

  1. void HeapAdjust(int H[],int start,int end)//堆调整,将start和end之间的元素调整为最大堆
  2. void HeapSort(int H[],int L,int R)//堆排序
  • 1
  • 2

核心代码实现:

  1. void HeapAdjust(int H[],int start,int end)//堆调整,将startend之间的元素调整为最大堆
  2. {
  3. int temp=H[start];
  4. int parent=start,child;
  5. while(2*parent<=end)
  6. {
  7. child=2*parent;
  8. if(child!=end&&H[child]<H[child+1])++child;
  9. if(temp>H[child])break;
  10. else H[parent]=H[child];
  11. parent=child;
  12. }
  13. H[parent]=temp;
  14. }
  15. void HeapSort(int H[],int L,int R)
  16. {
  17. for(int i=(R-L+1)/2;i>=L;--i)//调整整个二叉树为最大堆
  18. HeapAdjust(H,i,R);
  19. for(int i=R;i>=L;--i)
  20. {
  21. swap(&H[L],&H[i]);
  22. HeapAdjust(H,L,i-1);
  23. }
  24. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

测试样例:

  1. #include <stdio.h>
  2. #define NA -1
  3. void swap(int *a,int *b)//该函数用于交换两个变量的值
  4. {
  5. int temp=*a;
  6. *a=*b;
  7. *b=temp;
  8. }
  9. void HeapAdjust(int H[],int start,int end)//堆调整,将start和end之间的元素调整为最大堆
  10. {
  11. int temp=H[start];
  12. int parent=start,child;
  13. while(2*parent<=end)
  14. {
  15. child=2*parent;
  16. if(child!=end&&H[child]<H[child+1])++child;
  17. if(temp>H[child])break;
  18. else H[parent]=H[child];
  19. parent=child;
  20. }
  21. H[parent]=temp;
  22. }
  23. void HeapSort(int H[],int L,int R)
  24. {
  25. for(int i=(R-L+1)/2;i>=L;--i)//调整整个二叉树为最大堆
  26. HeapAdjust(H,i,R);
  27. for(int i=R;i>=L;--i)
  28. {
  29. swap(&H[L],&H[i]);
  30. HeapAdjust(H,L,i-1);
  31. }
  32. }
  33. int main(){
  34. int A[]={NA,1,3,63,5,78,9,12,52,8};//1开始存入数据
  35. printf("Previous Arrary:");
  36. for(int i=1;i<=9;++i)
  37. printf(" %d",A[i]);
  38. HeapSort(A,1,9);
  39. printf("\nSorted Arrary:");
  40. for(int i=1;i<=9;++i)
  41. printf(" %d",A[i]);
  42. printf("\n");
  43. return 0;
  44. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

运行结果:

  1. Previous Arrary: 1 3 63 5 78 9 12 52 8
  2. Sorted Arrary: 1 3 5 8 9 12 52 63 78
  3. 请按任意键继续. . .
  • 1
  • 2
  • 3

另:推荐一篇比较详细的文章,读者可以参看堆排序 Heap Sort

转载于:https://www.cnblogs.com/xLester/p/7570376.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/697395
推荐阅读
相关标签
  

闽ICP备14008679号