• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Sheppard Y

            keep thinking keep coding.

            堆排序

                這些天復習算法。


            heap_tree.h
             1 //Author: sheppard(ysf1026@gmail.com) 2013-04-06
             2 //              Desc:
             3 
             4 #ifndef __Atmlib_HeadTree_H__
             5 #define __Atmlib_HeadTree_H__
             6 
             7 #include <vector>
             8 
             9 class HeapTree
            10 {
            11 protected:
            12         std::vector<int> data_;
            13         int heap_size_;
            14 
            15 protected:
            16         virtual void HeapFix(int index) = 0;
            17 
            18         void Swap(int index1, int index2);
            19 
            20 public:
            21         HeapTree(int* array, int length);
            22 
            23         int heap_size() const;
            24         void PrintHeapData();
            25         void PrintData();
            26 
            27         int GetRightSonIndex(int index);
            28         int GetLeftSonIndex(int index);
            29         int GetParentIndex(int index);
            30 
            31         void BuildHeapTree();
            32         virtual void Insert(int value) = 0;
            33 };
            34 
            35 class MaxHeapTree : public HeapTree
            36 {
            37 protected:
            38         void HeapFix(int index);
            39 public:
            40         MaxHeapTree(int* array, int length):HeapTree(array, length){}
            41         void Insert(int value);
            42         int ExtractMax();
            43         void IncreaseKey(int index, int new_value);
            44 };
            45 
            46 #endif

            heap_tree.cpp
              1 //Author: sheppard(ysf1026@gmail.com) 2013-04-06
              2 //              Desc:
              3 
              4 #include <iostream>
              5 #include "heap_tree.h"
              6 
              7 HeapTree::HeapTree(int* array, int length)
              8 {
              9         heap_size_ = length;
             10         for(int i=0; i!=length; ++i)
             11                 data_.push_back(array[i]);
             12 }
             13 
             14 int HeapTree::heap_size() const
             15 {
             16         return heap_size_;
             17 }
             18 
             19 void HeapTree::PrintHeapData()
             20 {
             21         for(int i=0; i!=heap_size_; ++i)
             22         {
             23                 std::cout<<data_[i]<<" ";
             24         }
             25         std::cout<<std::endl;
             26 }
             27 
             28 void HeapTree::PrintData()
             29 {
             30         for(int i=0; i!=data_.size(); ++i)
             31         {
             32                 std::cout<<data_[i]<<" ";
             33         }
             34         std::cout<<std::endl;
             35 }
             36 
             37 int HeapTree::GetRightSonIndex(int index)
             38 {
             39         return (index + 1) * 2;
             40 }
             41 int HeapTree::GetLeftSonIndex(int index)
             42 {
             43         return GetRightSonIndex(index) - 1;
             44 }
             45 int HeapTree::GetParentIndex(int index)
             46 {
             47         return index / 2 - (index%2==0 ? 1 : 0);
             48 }
             49 
             50 void HeapTree::BuildHeapTree()
             51 {
             52         if(0 == data_.size())
             53                 return;
             54         int i = GetParentIndex(data_.size()-1);
             55         for(; i!=-1; --i)
             56         {
             57                 HeapFix(i);
             58         }
             59 }
             60 
             61 void HeapTree::Swap(int index1, int index2)
             62 {
             63         int tmp = data_[index1];
             64         data_[index1] = data_[index2];
             65         data_[index2] = tmp;
             66 }
             67 
             68 void MaxHeapTree::HeapFix(int index)
             69 {
             70         if(index >= heap_size())
             71                 return;
             72         int left = GetLeftSonIndex(index);
             73         int right = GetRightSonIndex(index);
             74         int largest = index;
             75 
             76         if(left<heap_size() && data_[left]>data_[largest])
             77                 largest = left;
             78         if(right<heap_size() && data_[right]>data_[largest])
             79                 largest = right;
             80 
             81         if(largest == index)
             82                 return;
             83 
             84         Swap(largest, index);
             85         HeapFix(largest);
             86 }
             87 
             88 void MaxHeapTree::Insert(int value)
             89 {
             90         //TODO
             91 }
             92 
             93 int MaxHeapTree::ExtractMax()
             94 {
             95         if(heap_size_ < 1)
             96         {
             97                 //TODO: assert?
             98                 return -1;
             99         }
            100 
            101         int max = data_[0];
            102         data_[0] = data_[heap_size_-1];
            103         HeapFix(0);
            104         return max;
            105 }
            106 
            107 void MaxHeapTree::IncreaseKey(int index, int new_value)
            108 {
            109         //TODO
            110 }

            heap_sort.h
             1 //Author: sheppard(ysf1026@gmail.com) 2013-04-06
             2 //              Desc:
             3 
             4 #ifndef __Atmlib_HeapSort_H__
             5 #define __Atmlib_HeapSort_H__
             6 
             7 #include "heap_tree.h"
             8 
             9 class HeapSort : public MaxHeapTree
            10 {
            11 private:
            12         void Sort();
            13 
            14 public:
            15         HeapSort(int* array, int length);
            16 };
            17 
            18 #endif

            heap_sort.cpp
             1 //Author: sheppard(ysf1026@gmail.com) 2013-04-08
             2 //              Desc:
             3 
             4 #include "heap_sort.h"
             5 
             6 HeapSort::HeapSort(int* array, int length):MaxHeapTree(array, length)
             7 {
             8         BuildHeapTree();
             9         Sort();
            10 }
            11 
            12 void HeapSort::Sort()
            13 {
            14         for( ; --heap_size_>0; )
            15         {
            16                 Swap(0, heap_size_);
            17                 HeapFix(0);
            18         }
            19 }

            posted on 2013-04-08 19:29 Sheppard Y 閱讀(307) 評論(0)  編輯 收藏 引用 所屬分類: 算法

            <2013年4月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導航

            統計

            留言簿(1)

            隨筆分類(77)

            隨筆檔案(58)

            me

            基友

            同行

            業界前輩

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            无码乱码观看精品久久| WWW婷婷AV久久久影片| 久久精品国产亚洲5555| 亚洲精品tv久久久久久久久久| 亚洲精品无码专区久久久 | 国产亚洲精久久久久久无码77777| 日产精品久久久久久久| 大伊人青草狠狠久久| 久久久久无码国产精品不卡| 亚洲精品无码久久千人斩| 99精品久久久久久久婷婷| 国产精品乱码久久久久久软件| 99久久久精品免费观看国产| 久久久久国产精品三级网| 久久久久久毛片免费播放| 看全色黄大色大片免费久久久| 伊人久久大香线蕉av一区| 久久人人爽人人爽AV片| 精品久久777| 久久夜色精品国产网站| 久久精品日日躁夜夜躁欧美| 久久久91人妻无码精品蜜桃HD| 久久777国产线看观看精品| 欧美日韩精品久久久免费观看| 久久久久九国产精品| 伊人久久免费视频| 国产午夜福利精品久久2021| 无码AV波多野结衣久久| 免费精品久久天干天干| 久久国产一片免费观看| 国产亚洲精久久久久久无码AV| 97精品伊人久久大香线蕉app | 国产精品久久久久久久午夜片| 久久99热只有频精品8| 久久婷婷国产综合精品| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久久www免费人成精品| 日批日出水久久亚洲精品tv| 久久久久亚洲爆乳少妇无| 久久精品无码一区二区三区日韩| 91精品国产91久久久久久蜜臀|