• <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
            數據加載中……

            [算法]找出m個數中最小的n個數

            這個問題屬于常見問題了,我的辦法是采用堆。

            截取STL中的partial_sort算法的實現:

            template <class RandomAccessIterator, class T, class Compare>
            void __partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
                                RandomAccessIterator last, T
            *, Compare comp) {
              make_heap(first, middle, comp);
              
            for (RandomAccessIterator i = middle; i < last; ++i)
                
            if (comp(*i, *first))
                  __pop_heap(first, middle, i, T(
            *i), comp, distance_type(first));
              sort_heap(first, middle, comp);
            }


            上面這個函數是對一個序列進行部分排序,排序之后,在first,middle之間的元素有序。
            它的做法首先將原始的first,middle中的數據生成一個堆,然后遍歷之后的數據,如果比前面的元素中最大的元素大,就把最小的元素刪除插入這個新的元素,這個過程是lg(middle - first),最后進行堆排序。

            不是每個元素都需要重新調整堆,所以最壞的情況時間復雜度是n * lg(middle - first)。

            類似的,可以根據這個算法并且使用堆算法解決這個問題,如上所言,最壞的時間復雜度是n * lg(m),其中n是元素總量,m是待查找的最大的m個數。

            根據我上面的這個想法, 還有我原來寫過的堆算法,給出解決這個問題的代碼,由于我原來寫的堆算法是大項堆(max-heap),也就是堆中最大的元素在根部,所以這里面求出來的是數中最小的10個數,如果要求最大的10個數,要講這個堆改成小項堆,其實也就是改變堆中父子之間的大小關系罷了,其他的地方不變。

             


            /********************************************************************
                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);

            // 用于根據堆的性質調整堆, 將nValue放到位置nHoleIndex, 并且保證堆性質的成立
            void    adjust_heap(int *pFirst, int nHoleIndex, int nLen, int nValue);

            // 得到一個數組中最小的n個元素
            void    get_n_min(int *pArray, int nLen, int n);

            void    display_array(int *pArray, int nLength);

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

                get_n_min(Array, 10, 2);

                return 0;
            }

            void get_n_min(int *pArray, int nLen, int n)
            {
                int *pTmp = (int*)malloc(sizeof(int) * n);
                if (NULL == pTmp)
                {
                    perror("malloc error");
                    return;
                }

                int i;
                for (i = 0; i < n; ++i)
                    pTmp[i] = pArray[i];
                make_heap(pTmp, pTmp + n);
                display_array(pTmp, n);

                for (; i < nLen; ++i)
                {
                    if (pArray[i] < pTmp[0])
                         adjust_heap(pTmp, 0, n, pArray[i]);
                }

                // 最后對堆進行排序
                sort_heap(pTmp, pTmp + n);

                cout << "the min n elements of the array is:\n";

                // 打印堆中的數據
                display_array(pTmp, n);

                free(pTmp);
            }

            // 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是需要安插進入堆中的值
            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    display_array(int *pArray, int nLength)
            {
                for (int i = 0; i < nLength; ++i)
                    std::cout << pArray[i] << " ";
                std::cout << std::endl;
            }





             

            posted on 2007-11-26 18:54 那誰 閱讀(4176) 評論(2)  編輯 收藏 引用 所屬分類: 算法與數據結構

            評論

            # re: [算法]找出n個中最大的m個數  回復  更多評論   

            不錯,我所知道的另外一個算法,也是N*lg(M),似乎這就是最快了的吧。
            2007-11-27 13:36 | tangl_99

            # re: [算法]找出n個中最大的m個數  回復  更多評論   

            只是找10個數而已,不用這么復雜吧,不如直接用有序鏈表算了,構建和維護成本比堆的成本小多了。要是要找的數量偏大,用上堆還差不多。
            2007-11-27 16:39 | www.helpsoff.com.cn
            久久99精品国产一区二区三区| 日韩乱码人妻无码中文字幕久久| 狠狠色婷婷综合天天久久丁香| 国产成人精品久久二区二区| 国内精品久久久久久野外| 94久久国产乱子伦精品免费| 午夜精品久久久久9999高清| 99久久99久久| 午夜精品久久久久成人| 99国产精品久久久久久久成人热| 久久中文娱乐网| 久久国产AVJUST麻豆| 国产精品久久久久9999| 亚洲国产精品一区二区三区久久 | 久久噜噜电影你懂的| 日本国产精品久久| 久久se精品一区二区| 久久伊人精品一区二区三区| 9191精品国产免费久久| 久久无码专区国产精品发布| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久久精品久久久久特色影视| 18禁黄久久久AAA片| 久久97久久97精品免视看秋霞 | 久久国产精品99久久久久久老狼 | 久久精品综合网| 久久久久无码精品| 久久久综合九色合综国产| 久久这里只有精品18| 7777精品伊人久久久大香线蕉| 精品国产乱码久久久久久浪潮| 久久国产高潮流白浆免费观看| 伊人色综合九久久天天蜜桃| 色天使久久综合网天天| 国产免费久久精品丫丫| 青青草国产精品久久| 伊人久久综合热线大杳蕉下载| 2021少妇久久久久久久久久| 国内精品伊人久久久久av一坑| 蜜臀av性久久久久蜜臀aⅴ| 人妻久久久一区二区三区|