• <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>

            那誰的技術博客

            感興趣領域:高性能服務器編程,存儲,算法,Linux內核
            隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
            數據加載中……

            仿STL中的堆算法的一個實現

            RT。
            堆的性質之類的不再這里闡述,寫這個算法只為了更好的理解STL中的堆算法,如果看不懂STL中的算法也可以來參考這里給出的算法,因為是純C的看起來會省去很多語言方面的細節。
            同時里面還有一個STL中對應算法的測試以比較兩者的效果。

            /********************************************************************
                created:    2007/3/18
                filename:     main.cpp
                author:        Lichuang
               
                purpose:    測試模擬堆算法
            *********************************************************************/

            #include <algorithm>
            #include <iostream>
            #include <time.h>

            using namespace std;

            // push_heap為向堆中添加一個新的元素, 調用這個算法的前提是[First, Last)之間的元素滿足堆的條件
            // 新加入的元素為Last
            void    push_heap(int* pFirst, int* pLast);

            // pop_heap為從堆中刪除一個元素, 調用這個算法的前提是[First, Last)之間的元素滿足堆的條件
            // 被刪除的元素被放置到Last - 1位置,由于這里是max-heap,所以被刪除的元素是這個序列中最大的元素
            void    pop_heap(int* pFirst, int* pLast);

            // make_heap將序列[First, Last)中的元素按照堆的性質進行重組
            void    make_heap(int* pFirst, int* pLast);

            // 對堆進行排序, 調用這個函數可以成功排序的前提是[pFirst, pLast)中的元素符合堆的性質
            void    sort_heap(int* pFirst, int* pLast);

            // 判斷一個序列[First, Last)是否滿足堆的條件,是就返回1,否則返回0
            char    is_heap(int* pFirst, int* pLast);

            void    test_heap_algo(int *pArray, int nLength);
            void    test_heap_algo_in_stl(int *pArray, int nLength);
            void    display_array(int *pArray, int nLength);

            int main()
            {
                srand(time(NULL));
                int Array[10], Array2[10];
                for(int i = 0; i < 10; ++i)
                    Array[i] = Array2[i] = rand();

                test_heap_algo(Array, sizeof(Array) / sizeof(int));
                test_heap_algo_in_stl(Array2, sizeof(Array2) / sizeof(int));

                return 0;
            }

            // 靜態函數, 用于根據堆的性質調整堆
            static void adjust_heap(int *pFirst, int nHoleIndex, int nLen, int nValue);

            // push_heap為向堆中添加一個新的元素, 調用這個算法的前提是[First, Last)之間的元素滿足堆的條件
            // 新加入的元素為Last
            void push_heap(int* pFirst, int* pLast)
            {
                int nTopIndex, nHoleIndex, nParentIndex;
                int nValue;

                nTopIndex = 0;
                nHoleIndex = (int)(pLast - pFirst - 1);
                nParentIndex = (nHoleIndex - 1) / 2;
                nValue = *(pLast - 1);
                // 如果需要插入的節點值比父節點大, 上溯繼續查找
                while (nHoleIndex > nTopIndex && pFirst[nParentIndex] < nValue)
                {
                    pFirst[nHoleIndex] = pFirst[nParentIndex];
                    nHoleIndex = nParentIndex;
                    nParentIndex = (nHoleIndex - 1) / 2;
                }
                pFirst[nHoleIndex] = nValue;
            }

            // pop_heap為從堆中刪除一個元素, 調用這個算法的前提是[First, Last)之間的元素滿足堆的條件
            // 被刪除的元素被放置到Last - 1位置,由于這里是max-heap,所以被刪除的元素是這個序列中最大的元素
            void pop_heap(int* pFirst, int* pLast)
            {
                int nValue;

                nValue = *(pLast - 1);
                *(pLast - 1) = *pFirst;
                adjust_heap(pFirst, 0, (int)(pLast - pFirst - 1), nValue);
            }

            // make_heap將序列[First, Last)中的元素按照堆的性質進行重組
            void make_heap(int* pFirst, int* pLast)
            {
                int nLen, nParentIndex;

                nLen = (int)(pLast - pFirst);
                nParentIndex = (nLen - 1) / 2;

                while (true)
                {
                    // 對父節點進行調整, 把父節點的值調整到合適的位置
                    adjust_heap(pFirst, nParentIndex, nLen, pFirst[nParentIndex]);
                    if (0 == nParentIndex)
                        return;
                    nParentIndex--;
                }
            }

            // 對堆進行排序, 調用這個函數可以成功排序的前提是[pFirst, pLast)中的元素符合堆的性質
            void sort_heap(int* pFirst, int* pLast)
            {
                // 調用pop_heap函數, 不斷的把當前序列中最大的元素放在序列的最后
                while(pLast - pFirst > 1)
                    pop_heap(pFirst, pLast--);
            }

            // 判斷一個序列[First, Last)是否滿足堆的條件,是就返回1,否則返回0
            char is_heap(int* pFirst, int* pLast)
            {
                int nLen, nParentIndex, nChildIndex;

                nLen = (int)(pLast - pFirst);
                nParentIndex = 0;
                for (nChildIndex = 1; nChildIndex < nLen; ++nChildIndex)
                {
                    if (pFirst[nParentIndex] < pFirst[nChildIndex])
                        return 0;

                    // 當nChildIndex是偶數時, 那么父節點已經和它的兩個子節點進行過比較了
                    // 將父節點遞增1
                    if ((nChildIndex & 1) == 0)
                        ++nParentIndex;
                }

                return 1;
            }

            // 一個靜態函數僅供adjust_heap調用以證實JJHOU的結論
            static void push_heap(int *pFirst, int nHoleIndex, int nTopIndex, int nValue)
            {
                int nParentIndex;

                nParentIndex = (nHoleIndex - 1) / 2;
                while (nHoleIndex > nTopIndex && pFirst[nParentIndex] < nValue)
                {
                    pFirst[nHoleIndex] = pFirst[nParentIndex];
                    nHoleIndex = nParentIndex;
                    nParentIndex = (nHoleIndex - 1) / 2;
                }
                pFirst[nHoleIndex] = nValue;
            }

            // 對堆進行調整, 其中nHoleIndex是目前堆中有空洞的節點索引, nLen是待調整的序列長度
            // nValue是需要安插進入堆中的值
            static void adjust_heap(int *pFirst, int nHoleIndex, int nLen, int nValue)
            {
                int nTopIndex, nSecondChildIndex;

                nTopIndex = nHoleIndex;
                nSecondChildIndex = 2 * nTopIndex + 2;
                while (nSecondChildIndex < nLen)
                {
                    if (pFirst[nSecondChildIndex] < pFirst[nSecondChildIndex - 1])
                        --nSecondChildIndex;
                    pFirst[nHoleIndex] = pFirst[nSecondChildIndex];
                    nHoleIndex = nSecondChildIndex;
                    nSecondChildIndex = 2 * nHoleIndex + 2;
                }
                if (nSecondChildIndex == nLen)
                {
                    pFirst[nHoleIndex] = pFirst[nSecondChildIndex - 1];
                    nHoleIndex = nSecondChildIndex - 1;
                }

                // 以下兩個操作在這個函數中的作用相同, 證實了<<STL源碼剖析>>中P178中JJHOU所言
                //pFirst[nHoleIndex] = nValue;
                push_heap(pFirst, nHoleIndex, nTopIndex, nValue);
            }

            void    test_heap_algo(int *pArray, int nLength)
            {
                std::cout << "\ntest_heap_algo()\n";
                make_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);

                push_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);

                pop_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);

                if (is_heap(pArray, pArray + nLength - 1))
                {
                    std::cout << "is heap!\n";
                }
                else
                {
                    std::cout << "is not heap!\n";
                }

                make_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);

                if (is_heap(pArray, pArray + nLength))
                {
                    std::cout << "is heap!\n";
                }
                else
                {
                    std::cout << "is not heap!\n";
                }

                sort_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);
            }

            void    test_heap_algo_in_stl(int *pArray, int nLength)
            {
                std::cout << "\ntest_heap_algo_in_stl()\n";

                std::make_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);

                std::push_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);

                std::pop_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);

                // 注意is_heap不是STL中支持的算法, 貌似只有SGI的實現才有這個函數!
                if (is_heap(pArray, pArray + nLength - 1))
                {
                    std::cout << "is heap!\n";
                }
                else
                {
                    std::cout << "is not heap!\n";
                }

                std::make_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);

                if (is_heap(pArray, pArray + nLength))
                {
                    std::cout << "is heap!\n";
                }
                else
                {
                    std::cout << "is not heap!\n";
                }

                std::sort_heap(pArray, pArray + nLength);
                display_array(pArray, nLength);
            }

            void    display_array(int *pArray, int nLength)
            {
                for (int i = 0; i < nLength; ++i)
                    std::cout << pArray[i] << " ";
                std::cout << std::endl;
            }



             

             

            posted on 2007-03-20 00:28 那誰 閱讀(2662) 評論(0)  編輯 收藏 引用 所屬分類: C\C++算法與數據結構

            精品国产乱码久久久久久郑州公司| 婷婷久久综合九色综合绿巨人| 无码人妻久久一区二区三区免费| 香蕉久久AⅤ一区二区三区| 久久午夜无码鲁丝片午夜精品| 综合久久精品色| 91精品国产9l久久久久| 久久婷婷五月综合色99啪ak| 亚洲中文字幕无码久久2017| 国内精品久久久久影院日本 | 久久综合九色欧美综合狠狠| 色妞色综合久久夜夜| 久久综合久久综合久久综合| 97精品依人久久久大香线蕉97| 久久青青草原国产精品免费| 精品久久久久成人码免费动漫| 青青青国产精品国产精品久久久久| 久久精品国产精品亚洲精品| 久久久精品国产Sm最大网站| 久久99国产精品久久99| 亚洲AV无码久久精品蜜桃| 久久综合一区二区无码| 国产亚洲色婷婷久久99精品91| 久久国产欧美日韩精品| 日日躁夜夜躁狠狠久久AV| 国产精品久久久久久久久久影院| 国产精品免费久久久久电影网| 99久久精品费精品国产一区二区 | 人妻精品久久无码专区精东影业| 国内精品久久久久国产盗摄| 一本大道久久a久久精品综合| 久久精品无码专区免费青青| 色综合久久无码中文字幕| 亚洲综合日韩久久成人AV| 7777精品伊人久久久大香线蕉| 久久综合精品国产一区二区三区 | 久久人爽人人爽人人片AV | 久久一区二区三区免费| 精品无码久久久久久久动漫| 99久久免费只有精品国产| 国产激情久久久久影院老熟女免费|