堆排序-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 752914
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1150
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-17 14:32
个人简介

小公司研发总监,既当司令也当兵!

文章分类

全部博文(144)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: linux

2016-02-16 22:15:00

二叉堆的定义

二叉堆是完全二叉树或者是近似完全二叉树。

二叉堆满足二个特性:

1.父结点的键值总是大于或等于(小于或等于)任何一个子节点的键值。

2.每个结点的左子树和右子树都是一个二叉堆(都是最大堆或最小堆)。

当父结点的键值总是大于或等于任何一个子节点的键值时为最大堆。当父结点的键值总是小于或等于任何一个子节点的键值时为最小堆。下图展示一个最小堆:

由于其它几种堆(二项式堆,斐波纳契堆等)用的较少,一般将二叉堆就简称为堆。

堆的操作一般包括初始化、插入和删除:

下面是堆的简单实现:

点击(此处)折叠或打开

  1. typedef struct struct_heap {

  2.     int size;
  3.     int head;
  4.     int *data;
  5. }heap_t;

  6. void initheap(heap_t **heap, int size)
  7. {
  8.     heap_t *pheap;
  9.     
  10.     pheap = (heap_t*)malloc(sizeof(*pheap) size * sizeof(int));
  11.     if (null == pheap)
  12.     {
  13.         printf("malloc fail\n");
  14.         return;
  15.     }
  16.     memset(pheap, 0, sizeof(*pheap) size * sizeof(int));

  17.     pheap->size = size;
  18.     pheap->head = 0;
  19.     pheap->data = (int *) ((char *)pheap sizeof(*pheap));

  20.     *heap = pheap;
  21. }

  22. void insertheap(heap_t *pheap, int key)
  23. {
  24.     int cursor;
  25.     int tmp;

  26.     if (null == pheap)
  27.         return;

  28.     if (pheap->head == pheap->size)
  29.     {
  30.         printf("heap is full\n");
  31.         return;
  32.     }

  33.     pheap->data[pheap->head] = key;

  34.     cursor = pheap->head - 1;

  35.     while (cursor > 0)
  36.     {
  37.         if (pheap->data[cursor] < pheap->data[(cursor - 1)/2])
  38.         {
  39.             tmp = pheap->data[cursor];
  40.             pheap->data[cursor] = pheap->data[(cursor-1) /2];
  41.             pheap->data[(cursor-1) / 2] = tmp;
  42.             cursor = (cursor - 1) / 2;
  43.         }
  44.         else
  45.             break;
  46.     }
  47. }

  48. void deleteheap(heap_t *pheap, int *out)
  49. {
  50.     int cursor;
  51.     int temp;
  52.     int left;
  53.     int right;
  54.     int key;

  55.     if (null == pheap)
  56.         return;

  57.     if (pheap->head == 0)
  58.     {
  59.         printf("heap is empty\n");
  60.         return;
  61.     }

  62.     *out = pheap->data[0];
  63.     pheap->data[0] = pheap->data[pheap->head - 1];
  64.     pheap->head--;

  65.     cursor = 0;
  66.     while (cursor < pheap->head -1)
  67.     {
  68.         key = pheap->data[cursor];
  69.         left = key < 0 ? (key * -2) : (key * 2 1);
  70.         right = left 1;

  71.         if ( (2 * cursor 1) <= pheap->head -1)
  72.         {
  73.             left = pheap->data[2*cursor 1];
  74.         }
  75.         if ( (2 * cursor 2) <= pheap->head -1)
  76.         {
  77.             right = pheap->data[2*cursor 2];
  78.         }

  79.         if (key > left && left < right)
  80.         {
  81.             temp = pheap->data[cursor];
  82.             pheap->data[cursor] = pheap->data[2 * cursor 1];
  83.             pheap->data[2 * cursor 1] = temp;
  84.             cursor = 2 * cursor 1;
  85.         }
  86.         else if (key > right && right < left)
  87.         {
  88.             temp = pheap->data[cursor];
  89.             pheap->data[cursor] = pheap->data[2 * cursor 2];
  90.             pheap->data[2 * cursor 2] = temp;
  91.             cursor = 2 * cursor 2;
  92.         }
  93.         else
  94.             break;
  95.     }
  96. }


堆排序
理解堆的插入和删除后,堆排序实现就非常简单了。下面是堆排序的简单实现:

点击(此处)折叠或打开

  1. // 在index位置插入新元素后,修正堆序列使之满足堆要求
  2. static void minheapfixup(int list[], int index)
  3. {
  4.     int cursor;
  5.     int temp;

  6.     cursor = index;
  7.     while (cursor > 0)
  8.     {
  9.         if (list[cursor] < list[(cursor -1)/2])
  10.         {
  11.             temp = list[cursor];
  12.             list[cursor] = list[(cursor-1)/2];
  13.             list[(cursor-1)/2] = temp;
  14.             cursor = (cursor - 1) / 2;
  15.         }
  16.         else
  17.             break;
  18.     }
  19. }

  20. // 删除堆顶元素后,将最后一个元素移至堆顶,修正堆序列使之满足堆要求
  21. static void minheapfixdown(int list[], int len)
  22. {
  23.     int cursor;
  24.     int temp;

  25.     int key;
  26.     int left;
  27.     int right;

  28.     cursor = 0;

  29.     while (cursor < len)
  30.     {
  31.         key = list[cursor];
  32.         
  33.         if ( 2 * cursor 1 < len)
  34.             left = list[2 * cursor 1];
  35.         else
  36.             left = key 1;

  37.         if ( 2 * cursor 2 < len)
  38.             right = list[2 * cursor 2];
  39.         else
  40.             right = key 2;

  41.         if (key > left && left < right)
  42.         {
  43.             temp = key;
  44.             list[cursor] = left;
  45.             list[cursor * 2 1] = temp;
  46.             cursor = cursor * 2 1;
  47.         }
  48.         else if (key > right && right < left)
  49.         {
  50.             temp = key;
  51.             list[cursor] = right;
  52.             list[cursor * 2 2] = temp;
  53.             cursor = cursor * 2 2;
  54.         }
  55.         else
  56.             break;
  57.     }
  58. }

  59. void heapsort(int list[], int len)
  60. {
  61.     int i;
  62.     int temp;

  63.     for (i = 1; i < len; i)
  64.         minheapfixup(list, i);

  65.     for (i = len; i > 1; i--)
  66.     {
  67.         temp = list[0];
  68.         list[0] = list[i - 1];
  69.         list[i - 1] = temp;

  70.         minheapfixdown(list, i - 1);
  71.     }
  72. }
注意这里采用的是最小堆,排序后数组是按递减顺序排列的;如果需要递增顺序,修改为最大堆,或者再进行一次倒置。
阅读(1783) | 评论(0) | 转发(0) |
0

上一篇:选择排序

下一篇:冒泡排序

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图