• <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 那誰 閱讀(4191) 評論(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
            亚洲人成网站999久久久综合 | 国产Av激情久久无码天堂| 日韩久久久久久中文人妻| 2020久久精品国产免费| 草草久久久无码国产专区| 亚洲人成网站999久久久综合| 日本加勒比久久精品| 久久中文骚妇内射| 日本精品久久久久久久久免费| 久久精品中文字幕一区| 99久久国产主播综合精品| 欧美精品九九99久久在观看| 久久久精品人妻一区二区三区四 | 国产亚洲综合久久系列| 国产精品无码久久综合网| 亚洲精品乱码久久久久久久久久久久 | 久久精品www| 一级a性色生活片久久无少妇一级婬片免费放| 久久精品国产免费观看三人同眠| segui久久国产精品| 亚洲欧美成人综合久久久| 亚洲国产精品综合久久网络| 国产一区二区精品久久| 久久亚洲私人国产精品| 99久久无色码中文字幕人妻| 久久AAAA片一区二区| 91精品国产综合久久四虎久久无码一级 | 亚洲精品无码成人片久久| 草草久久久无码国产专区| 狠狠狠色丁香婷婷综合久久俺| 欧美伊人久久大香线蕉综合| 久久精品国产精品亜洲毛片| 国产精品久久亚洲不卡动漫| 麻豆亚洲AV永久无码精品久久| 久久精品国产亚洲AV忘忧草18| 无码任你躁久久久久久久| 亚洲国产成人久久综合区| 亚洲婷婷国产精品电影人久久| 亚洲综合久久夜AV | 久久99这里只有精品国产| 精品久久亚洲中文无码|