Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19216
  • 博文数量: 4
  • 博客积分: 117
  • 博客等级: 入伍新兵
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-26 21:04
文章分类
文章存档

2011年(4)

我的朋友

分类: C/C++

2011-07-08 12:13:04

  1. #include <stdio.h>

  2. void print_arry(const char *tips, const int *str, int length)
  3. {
  4.     int i;
  5.     
  6.     printf("%s\n\t", tips);
  7.     
  8.     for (i = 0; i < length; i++)
  9.     {
  10.         printf("%-8d", str[i]);
  11.     }
  12.     printf("\n");
  13.     
  14.     return;
  15. }

  16. void swap(int *array, int left, int right)
  17. {
  18.     int tmpval;
  19.     if(left == right)
  20.         return;
  21.         
  22.     if(array[left] == array[right])
  23.         return;
  24.     
  25.     tmpval = array[left];
  26.     array[left] = array[right];
  27.     array[right] = tmpval;
  28.     
  29.     return;
  30. }

  31. int get_left_child_node(int node)
  32. {
  33.     return node * 2 + 1;
  34. }

  35. int get_right_child_node(int node)
  36. {
  37.     return node * 2 + 2;
  38. }

  39. int get_parent_node(int node)
  40. {
  41.     if(node == 0)
  42.         return -1;
  43.     return (node-1)/2;
  44. }

  45. void reorganized_max_heap(int *array, int node, int heap_size)
  46. {
  47.     int left, right, parent;
  48.     int chg_node = node;
  49.     
  50.     if(node == -1)
  51.         return;
  52.     
  53.     left = get_left_child_node(node);
  54.     right = get_right_child_node(node);
  55.     parent = get_parent_node(node);
  56.     
  57.     /**
  58.      * if left child node bigger than heap size then return
  59.      */
  60.     if(left <= heap_size)
  61.     {
  62.         if(right > heap_size)
  63.         {
  64.             if(array[node] < array[left])
  65.                 chg_node = left;
  66.                 
  67.             if(chg_node != node)
  68.             {
  69.                 swap(array, node, chg_node);
  70.             }
  71.             
  72.             reorganized_max_heap(array, parent, heap_size);
  73.         }
  74.         else
  75.         {
  76.             if((array[right] > array[left]) && (array[right] > array[node]))
  77.                 chg_node = right;
  78.             
  79.             if((array[left] > array[right]) && (array[left] > array[node]))
  80.                 chg_node = left;
  81.             
  82.             if(chg_node != node)
  83.             {
  84.                 swap(array, node, chg_node);
  85.             }
  86.             reorganized_max_heap(array, parent, heap_size);
  87.         }
  88.             
  89.     }
  90. }

  91. void build_max_heap(int *array, int heap_size)
  92. {
  93.     int tmpnode = 0;
  94.     int mynode = 0;
  95.     int node = (heap_size-1)/2;
  96.     
  97.     while(mynode <= node)
  98.     {
  99.         for(tmpnode = node; tmpnode >= mynode; tmpnode--)
  100.         {
  101.             reorganized_max_heap(array, tmpnode, heap_size);
  102.         }
  103.         mynode++;
  104.         print_arry("build_max_heap",array, heap_size+1);
  105.     }
  106. }

  107. void heap_sort(int *array, int heap_size)
  108. {
  109.     int node;
  110.     
  111.     build_max_heap(array, heap_size);
  112.     swap(array, 0, heap_size);
  113.     
  114.     for(node = heap_size-1; node >= 0; node--)
  115.     {
  116.         print_arry("reorganized_max_heap before",array, heap_size+1);
  117.         reorganized_max_heap(array, (node-1)/2, node);
  118.         swap(array, 0, node);
  119.     }
  120. }

  121. int main(void)
  122. {
  123.     int length;
  124.     int array[] = {1, 10, 30, 40 ,2, 8, 50, 70, 6};
  125.     
  126.     length = sizeof(array)/sizeof(array[0]);
  127.     
  128.     print_arry("heap sort before print", array, length);
  129.     heap_sort(array, length-1);
  130.     print_arry("heap sort after print", array, length);
  131.     
  132.     return 0;
  133. }
阅读(657) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~