Chinaunix首页 | 论坛 | 博客
  • 博客访问: 291851
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 874
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-21 09:58
个人简介

traveling in cumputer science!!

文章分类

全部博文(82)

文章存档

2016年(13)

2015年(69)

我的朋友

分类: C/C++

2016-02-21 11:01:43

1.堆排序理论

本方法采用最大堆结构,最大堆是一棵完全二叉树也是一棵最大树,最大树是指在树中,若一个结点有儿子结点则其关键字值都不小于其儿子结点的关键字值。
根据需要排序的序列list,
(1)把list创建为最大堆结构,这一步不用添加额外存储空间,以list本身为堆的存储结构,从首元素开始逐个将元素加入堆中,每加入一个就要重新调整为最大堆,这样直到最后一个元素加入调整成与原list对应的最大堆;
(2)取堆顶元素,也就是(1)调整后的list的首元素,使其与堆list的末尾元素交换,这样就形成了除去最大元素的一个新堆,调整新堆为新的最大堆,这一过程中就不再考虑交换到末尾的旧的最大堆的堆顶元素,调整后就形成了比原list缺省了最大元素的新最大堆,按照这个规则每次都取新的最大堆的堆顶元素与新的最大堆的末尾元素交换,然后调整为比上一最大堆少一个元素的新最大堆,直到所有list元素取完,原list就形成了一个升序的排列

2.C++ 代码实现:

堆操作:

点击(此处)折叠或打开

  1. /*****************************
  2. filename: HeapManage.cpp
  3. description: realize heap operate in CPP
  4.                 like, create heap , modify heap
  5. author: warrior mail:15933533880@163.com
  6. date:2016-2-20
  7. log:
  8. *****************************/

  9. /****** include *******/
  10. #include "heapManage.h"
  11. using namespace std;
  12. /****** define *********/
  13. /****** variable ********/
  14. /****** function *******/
  15. /****************
  16. description: sort a int list by heap sort
  17. input:
  18.     int * arrIN
  19.     const int arrLength
  20. output:
  21.     none
  22. ****************/
  23. void heapSort(int* arrIN, const int arrLength)
  24. {
  25.     int heapLength = arrLength;
  26.     int tempItem;
  27.     createHeap(arrIN, arrLength);
  28.     for(int i=0; i<arrLength; i++)
  29.     {
  30.         deleteMaxHeap(arrIN, heapLength);
  31.     }
  32.     for(int i=0; i<arrLength; i++)
  33.     {
  34.         cout<<" ,"<<arrIN[i];
  35.     }
  36.     cout<<endl;
  37. }
  38. /****************
  39. description: create heap by int list
  40. input:
  41.     int * arrIN
  42.     int arrLength
  43. output:
  44.     none
  45. ****************/
  46. void createHeap(int* arrIN, const int arrLength)
  47. {
  48.     int heapLength = 0;
  49.     for(int i=0; i<arrLength; i++)
  50.     {
  51.         insertMaxHeap(arrIN, arrIN[i], heapLength);
  52.     }
  53.     for(int i=0; i<arrLength; i++)
  54.     {
  55.          cout<<" ,"<<arrIN[i];
  56.     }
  57.     printf("lala\n");
  58. }
  59. /****************
  60. description: insert one item to max heap
  61. input:
  62.     int * Heap
  63.     int dataIN
  64.     int &heapLength
  65. output:
  66.     none
  67. ****************/
  68. void insertMaxHeap(int* Heap, int dataIN, int &heapLength)
  69. {
  70.     int iterationLength;
  71.     int parentItem;
  72.     int sunItem;
  73.     int tempItem;
  74.     heapLength++;
  75.     iterationLength = heapLength;
  76.     Heap[iterationLength-1] = dataIN;
  77.     if(iterationLength==1)
  78.         return;
  79.     else
  80.     {
  81.          while(iterationLength>1)
  82.         {
  83.             parentItem = Heap[iterationLength/2 - 1];
  84.             sunItem = Heap[iterationLength-1];
  85.             if(parentItem < sunItem)
  86.             {
  87.                 tempItem = parentItem;
  88.                 parentItem = sunItem;
  89.                 sunItem = tempItem;
  90.                 Heap[iterationLength/2 - 1] = parentItem;
  91.                 Heap[iterationLength-1] = sunItem;
  92.             }
  93.             else
  94.                 break;
  95.             iterationLength = iterationLength/2;
  96.         }
  97.     }
  98. }
  99. /****************
  100. description: delete one item to max heap
  101. input:
  102.     int * Heap
  103.     int &heapLength
  104. output:
  105.     none
  106. ****************/
  107. void deleteMaxHeap(int* Heap, int &heapLength)
  108. {
  109.     int iterationLength;
  110.     int parentItem;
  111.     int sunItem;
  112.     int tempItem;
  113.     tempItem = Heap[0];
  114.     Heap[0] = Heap[heapLength-1];
  115.     Heap[heapLength-1] = tempItem;
  116.     iterationLength = 1;
  117.     heapLength--;
  118.     while(iterationLength*2 <= heapLength)
  119.     {
  120.         if((iterationLength*2+1)<=heapLength)
  121.         {
  122.             if(Heap[iterationLength*2+1-1]>Heap[iterationLength*2-1])
  123.             {
  124.                 if(Heap[iterationLength*2+1-1]>Heap[iterationLength-1])
  125.                 {
  126.                     tempItem = Heap[iterationLength-1];
  127.                     Heap[iterationLength-1] = Heap[iterationLength*2+1-1];
  128.                     Heap[iterationLength*2+1-1] = tempItem;
  129.                     iterationLength = iterationLength*2+1;
  130.                 }
  131.                 else
  132.                     break;
  133.             }
  134.             else
  135.             {
  136.                 if(Heap[iterationLength*2-1]>Heap[iterationLength-1])
  137.                 {
  138.                     tempItem = Heap[iterationLength-1];
  139.                     Heap[iterationLength-1] = Heap[iterationLength*2-1];
  140.                     Heap[iterationLength*2-1] = tempItem;
  141.                     iterationLength = iterationLength*2;
  142.                 }
  143.                 else
  144.                     break;
  145.             }
  146.         }
  147.         else
  148.         {
  149.             if(Heap[iterationLength*2-1]>Heap[iterationLength-1])
  150.             {
  151.                 tempItem = Heap[iterationLength-1];
  152.                 Heap[iterationLength-1] = Heap[iterationLength*2-1];
  153.                 Heap[iterationLength*2-1] = tempItem;
  154.                 iterationLength = iterationLength*2;
  155.             }
  156.             else
  157.                 break;
  158.         }
  159.     }
  160.     for(int i=0; i<heapLength; i++)
  161.     {
  162.         cout<<" ,"<<Heap[i];
  163.     }
  164.     cout<<"lulu"<<endl;

  165. }

点击(此处)折叠或打开

  1. #ifndef _HEAPMANAGE_H_
  2. #define _HEAPMANAGE_H_
  3. /****** include *******/
  4. #include <iostream>
  5. #include <stdio.h>
  6. /****** define *********/
  7. /****** variable ********/
  8. /****** function *******/
  9. void heapSort(int* arrIN, const int arrLenght);
  10. void createHeap(int* arrIN, const int arrLength);
  11. void insertMaxHeap(int* Heap, int dataIN, int &heapLength);
  12. void deleteMaxHeap(int* Heap, int &heapLength);
  13. #endif // _HEAPMANAGE_H_

主函数:

点击(此处)折叠或打开

  1. #include "heapManage.h"

  2. using namespace std;

  3. int main()
  4. {
  5.     int arrIN[] = {98, 17, 38, 19, 22, 69, 45, 12, 65, 98};
  6.     heapSort(arrIN, sizeof(arrIN)/sizeof(int));
  7.     cout << "Hello world!" << endl;
  8.     return 0;
  9. }

3.总结

(1)最大堆的父节点与子节点间的关系挺有意思,依据这个关系很方便的进行堆调整
                i
             /     \
          2*i     2*i + 1
(2)C++ 函数参数引用传递方法与按值传递方法
    void fun1(int &variable);variable 参数变量可以在fun1中进行改变,并能体现在函数fun1的外部,原因就是函数参数的地址也指向了variable的地址,也就是不同的变量名指向了相同的地址;
    void fun2(int variable);variable 参数变量可以在fun2中进行改变,但不能体现在函数fun2的外部,原因就是参数传递过程是variable把自己的值传递给了函数的参数变量,两个变量指向的是不同的地址,函数内对variable的操作并不是函数外部的variable,因此不会影响到外部变量的值;
    void fun3(const int variable); fun3中参数是常参数传递格式,意义就是传入的参数相当于常数,在函数fun3中只能对其读取不能对其修改,除此之外const参数和普通按值传递的参数一样使用。

阅读(1910) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~