1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define CAPACITY 20
  4.  
  5. /*堆有两个性质:
  6. * 1.结构性:堆必须是一颗完全二叉树
  7. * 2.堆序性:堆的父节点要么都大于子节点,要么小于子节点,前者叫大顶堆,后者叫小顶堆;
  8. * 由此,堆可以用一个数组来表示,并有如下性质:
  9. * 1.对于任意i位置的元素,他的左子节点在2i位置,右子节点在2i+1位置;
  10. * 2.他的父节点(假如有)在i/2位置*/
  11.  
  12. /*创建一个小顶堆,size代表的是实际元素的个数*/
  13. typedef struct MinHeap {
  14. int size;
  15. int data[CAPACITY];
  16. } heap;
  17.  
  18. void init( heap *h );
  19. void insert(heap *h,int x);
  20. void travel(heap *h);
  21.  
  22. /*数组0位置要空着*/
  23. void init( heap *h ) {
  24. h->size=;
  25. }
  26.  
  27. void insert(heap *h,int x) {
  28. if(h->size == CAPACITY) {
  29. printf("heap is full!");
  30. return;
  31. }
  32. int i;
  33. h->size++;
  34. for(i=h->size; i>=; i/=) {
  35. if(x < h->data[i/]) {
  36. h->data[i]=h->data[i/];
  37. } else {
  38. break;
  39. }
  40. }
  41. h->data[i]=x;
  42. }
  43. /*删除最小元素,在小顶堆即意味着删除根节点
  44. * 1.首先将根元素保存,等待最后return;
  45. * 2.将最后一个元素赋值给根元素,并将这个值赋给缓冲区,这样保证了堆的结构性;
  46. * 3.从根节点开始遍历,比较父节点和两个子节点的大小,如果缓冲区值大于较小的子节点,则将小节点的值赋给父节点
  47. * 4.直到缓冲区值小于游标的两个子节点,此时将缓冲区值赋给游标所在位置*/
  48. int deleteMin(heap *h) {
  49. int child;
  50. int result=h->data[];
  51. h->data[]=h->data[h->size];
  52. h->size--;
  53. int i=;
  54. int temp=h->data[];
  55. for(i=; *i <= h->size; i=child) {
  56. child=*i;
  57. if(child !=h->size && h->data[child] > h->data[child+] ) {/*如果左子节点非最后元素且>右子节点,则右子节点最小*/
  58. child++;
  59. }
  60. if(temp > h->data[child]) {/*如果temp大于当前元素的最小子节点,则将最小子节点赋值给父节点,否则跳出*/
  61. h->data[i]=h->data[child];
  62. } else {
  63. break;
  64. }
  65. }
  66. h->data[i]=temp;/*将缓冲区值赋给当前游标*/
  67. return result;
  68. }
  69.  
  70. /*遍历堆数组:越过空白位置0,从1开始*/
  71. void travel(heap *h) {
  72. int i;
  73. for(i=; i<=h->size; i++) {
  74. printf("%d ",h->data[i]);
  75. }
  76. printf("\n");
  77. }
  78.  
  79. /*堆排序*/
  80. void heap_sort(int a[],int n) {
  81. int i;
  82. heap *h=(heap*)malloc(sizeof(heap));/*给堆指针分配空间*/
  83. init(h);/*初始化堆*/
  84. for(i=; i<n; i++) {/*将数组的元素依次插入堆*/
  85. insert(h,a[i]);
  86. }
  87. for(i=; i<n; i++) {
  88. a[i]=deleteMin(h);
  89. }
  90. }
  91. /*遍历数组*/
  92. void travel_array(int a[],int n) {
  93. int i;
  94. for(i=; i<n; i++) {
  95. printf("%d ",a[i]);
  96. }
  97. printf("\n");
  98. }
  99.  
  100. main() {
  101. int a[]= {,,,,,,,,,,,,,,,};
  102. int n=sizeof(a)/sizeof(int);
  103. travel_array(a,n);
  104. heap_sort(a,n);
  105. travel_array(a,n);
  106. }

C语言实现常用数据结构——堆的更多相关文章

  1. C语言实现常用数据结构——链表

    #include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; ...

  2. C语言实现常用数据结构——图

    #include<stdio.h> #include<stdlib.h> #define SIZE 20 #define LENGTH(a) (sizeof(a)/sizeof ...

  3. C语言实现常用数据结构——二叉树

    #include<stdio.h> #include<stdlib.h> #define SIZE 10 typedef struct Tree { int data; str ...

  4. C语言实现常用数据结构——队列

    #include<stdio.h> #include<stdlib.h> #define MAX_SIZE 10 /* 用一个动态数组来实现队列 */ typedef stru ...

  5. C语言实现常用数据结构——栈

    #include<stdio.h> #include<stdlib.h> //用链表实现栈 typedef struct Node { int data; struct Nod ...

  6. C++常用数据结构的实现

    常用数据结构与算法的实现.整理与总结 我将我所有数据结构的实现放在了github中:Data-Structures-Implemented-By-Me 常用数据结构与算法的实现.整理与总结 KMP字符 ...

  7. 1. C语言中的数据结构.md

    C语言内建数据结构类型 整型 整型数据是最基本的数据类型,不过从整形出发衍生出好几种integer-like数据结构,譬如字符型,短整型,整型,长整型.他们都是最基本的方式来组织的数据结构,一般是几位 ...

  8. 常用数据结构及复杂度 array、LinkedList、List、Stack、Queue、Dictionary、SortedDictionary、HashSet、SortedSet

    原文地址:http://www.cnblogs.com/gaochundong/p/data_structures_and_asymptotic_analysis.html  常用数据结构的时间复杂度 ...

  9. php常用数据结构

    # 常用数据结构--------------------------------------------------------------------------------## 树(Tree)- ...

随机推荐

  1. CAP和最终一致性

    查阅资料整理了最终一致性.CAP 相关的内容.由于图省事儿,没有做文字的整理记载,只有 slides 和一些查阅过的链接,大家将就着看.欢迎指正. slides: slides 链接:请戳这里 背景 ...

  2. automapper如何全局配置map条件过滤null值空值对所有映射起效

    原文 automapper如何全局配置map条件过滤null值空值对所有映射起效 我们在使用automapper的时候经常会遇到这样的问题:假设展示给用户的数据我们用UserDto类,User类就是我 ...

  3. linux下一个C语言要求CPU采用

    部分   从灾难中 本来我想写一个小程序来测试CPU其他工具利用它可以检验类数据的性能.以后参考IPbench中间cpu_target_lukem插件实现我们的功能.原理非常简单:就是我们给程序设置了 ...

  4. win7在USB该解决方案不健全音箱

    Win7安装后,原XP在正常工作USB小喇叭不工作,重新安装声卡驱动程序仍然是相同的.后来,通过以下的得心应手最后一次尝试. 1.右键右下角喇叭button. 2.点击"播放设备" ...

  5. linux之tail -F命令异常file truncated

    使用tail -F收集日志时,经常报出file truncated, 导致日志又重新读取.tail: `test.out' has appeared;  following end of new fi ...

  6. WPF - 本质:数据和行为

    原文:WPF - 本质:数据和行为 如果自己来做一个UI框架,我们会首先关注哪些方面?我想UI框架主要处理的一定包括两个主要层次的内容,一个是数据展现,另一个就是数据操作,所以UI框架必须能够接收各种 ...

  7. js 指向表格行变色,离开恢复

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  8. 数据绑定(七)使用ObjectDataProvider对象作为Binding的Source

    原文:数据绑定(七)使用ObjectDataProvider对象作为Binding的Source ObjectDataProvider就是把对象作为数据源提供给Binding,类似的还有XmlData ...

  9. Git 将子文件夹分离为一个新的库

    前面的需求 公司Android的项目上,想要将一些module抽取出来,作为一个可以被其它项目上使用的. 所以使用了git submodule的方案. 为了将代码库中的一个文件夹分离后,作为一个单独的 ...

  10. ADB 基础命令使用

    1.adb shell(>=2个设备显示:error: more than one device/emulator,仅连接一个设备可用) adb -d shell 只运行在真实设备中 adb - ...