• <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 閱讀(314) 評論(0)  編輯 收藏 引用 所屬分類: 算法

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            留言簿(1)

            隨筆分類(77)

            隨筆檔案(58)

            me

            基友

            同行

            業界前輩

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            色8久久人人97超碰香蕉987| 91精品国产乱码久久久久久| 国产成人精品久久一区二区三区av | 狠狠色丁香久久婷婷综合蜜芽五月| 三级三级久久三级久久| 久久精品国产精品亚洲精品| 久久久久亚洲精品天堂| 久久性精品| 午夜久久久久久禁播电影| 精品久久久久久无码国产| 一本色综合网久久| 国产香蕉97碰碰久久人人| 亚洲AV无码久久| 久久精品国产男包| 国内精品免费久久影院| 波多野结衣中文字幕久久| 色诱久久av| 久久国产精品无码网站| 成人综合伊人五月婷久久| 香蕉久久夜色精品国产2020| 97r久久精品国产99国产精| 国产成人久久精品一区二区三区| 国产亚洲成人久久| 77777亚洲午夜久久多喷| 热99RE久久精品这里都是精品免费| 久久久综合九色合综国产| 久久综合狠狠综合久久| 囯产极品美女高潮无套久久久| 国产精品久久久99| 中文字幕一区二区三区久久网站| 久久狠狠高潮亚洲精品| 亚洲中文字幕无码久久综合网| 一级女性全黄久久生活片免费| 色综合久久中文色婷婷| 久久91精品久久91综合| 久久精品国产亚洲av日韩| 日本强好片久久久久久AAA| 亚洲人成网亚洲欧洲无码久久 | 久久国产精品成人片免费| 欧美日韩精品久久久免费观看| 日批日出水久久亚洲精品tv|