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

            學習筆記

            C++、Linux

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              0 隨筆 :: 1 文章 :: 0 評論 :: 0 Trackbacks

            導讀:http://www.cnblogs.com/zhjp11/archive/2010/02/26/1674227.html

             

            現在要選擇第k小的數字,一種比較簡單的方法就是先排序,然后根據下標找出第k小的數字,這個時間復雜度為O(nlogn)

            selectKth有點類似于快速排序,不過他的時間復雜度為O(n)。(就是導讀中的解法3)

            下面是一個運行時間的對比圖,selectKth的運行時間有很明顯的優勢。

             



             

            main.cpp
            #include <iostream>
            #include <cstdlib>
            #include <algorithm>
            #include "Record.h"
            #include "Rand.h"
            #include "SelectKth.h"
            using namespace std;
            const int MaxSize=1000000;
            const int Step=5000;
            int main()
            {
                Record record;
                int A[MaxSize],B[MaxSize],C[MaxSize];
                int curLen,i,kth;
                for(curLen=Step;curLen<MaxSize;curLen+=Step)
                {
                    for(i=0;i<curLen;i++)
                    {
                        A[i]=RandIn(0,1234567);
                        B[i]=A[i];
                        C[i]=A[i];
                    }
                    kth=RandIn(0,curLen)+1;
                    cout<<curLen<<"\t";
                    //sort
                    record.StartRecord();
                    sort(B,B+curLen);
                    cout<<B[kth-1]<<"\t";
                    record.PrintCostTime();
                    //select kth
                    record.StartRecord();
                    cout<<"\t"<<SelectKth(C,0,curLen-1,kth)<<"\t";
                    record.PrintCostTime();
                    cout<<endl;
                }
                return 0;
            }

            Record.h
            #ifndef RECORD__HH
            #define RECORD__HH
            #include<iostream>
            #include<ctime>
            #include<fstream>
            #include<string>
            #include<cstring>
            #include<iomanip>
            using namespace std;

            class Record
            {

            public:

                Record()
                {
                    StartRecord();
                }
                void StartRecord()  {   startTime=clock();  }  /*重置開始時間*/
                void PrintCostTime()
                {
                    curTime=clock();
                    cout<<(curTime-startTime)/1000;
                }
                private:
                unsigned int startTime,curTime;
            };


            #endif

            Rand.h
            #ifndef RAND__HH__HH
            #define RAND__HH__HH
            #include<cstdlib>
            #include<ctime>
            using namespace std;
            int RandIn(int left,int right)
            {

                if(left>=right)
                    return left;
                int res=rand();
                res=res%(right-left);
                res+=left;
                return res;
            }
            #endif

            SelectKth.h

            template <class T>
            int SelectMiddle(T A[],int left,int right)
            {
                int middle=((left+right)>>1),p;
                if( A[left]<A[middle] )
                {
                    if(A[middle] <= A[right])
                        p=middle;
                    else    //A[middle] is the biggest
                        p=(A[left] < A[right]) ? right :left;
                }
                else    //A[left]>=A[middle]
                {
                    if(A[right]>A[left])
                        p=left;
                    else    //A[left] is the biggest
                        p=(A[middle]< A[right]) ? right :middle;
                }
                return p;
            }

            template <class T>
            T SelectKth(T A[],int left,int right,int kth)
            {
                int i, store;
                int p=SelectMiddle(A,left,right);
                swap(A[p],A[right]);
                store = left;
                for (i = left; i < right; i++)
                    if (A[i] <= A[right])
                        swap(A[store++], A[i]);
                swap(A[store], A[right]);
                if(store+1==kth)
                    return A[store];
                else if(store+1<kth)
                    return SelectKth(A,store+1,right,kth);
                else    //sotre+1>kth
                    return SelectKth(A,left,store-1,kth);
            }






            類別:算法 查看評論
            posted on 2011-01-21 22:51 YcdoiT 閱讀(333) 評論(0)  編輯 收藏 引用
            久久精品国产亚洲αv忘忧草 | 亚洲国产成人精品无码久久久久久综合| 久久精品中文字幕一区| 亚洲国产精品久久电影欧美| 色综合久久无码中文字幕| 伊人久久大香线蕉精品| 亚洲另类欧美综合久久图片区| 一本色道久久HEZYO无码| 久久香蕉超碰97国产精品 | 亚洲精品国产综合久久一线| 亚洲国产精品无码久久一线| 久久国产精品-国产精品| 色综合久久久久综合99| 久久午夜电影网| 欧美精品乱码99久久蜜桃| 97久久超碰成人精品网站| 国产精品中文久久久久久久| 国产欧美久久一区二区| 一级做a爰片久久毛片免费陪| 久久99精品久久久久婷婷| 四虎国产精品成人免费久久| 国产精品久久久久久久久鸭| 久久天天躁狠狠躁夜夜躁2014| 26uuu久久五月天| 久久久久亚洲av无码专区导航| 亚洲精品乱码久久久久久不卡| 日本精品久久久中文字幕| 久久久久久久久无码精品亚洲日韩 | 久久久久夜夜夜精品国产| 97久久婷婷五月综合色d啪蜜芽| 国内精品久久久久国产盗摄| 97久久天天综合色天天综合色hd | 久久亚洲AV成人无码电影| 久久精品国产免费观看| 人妻无码久久精品| 久久精品亚洲欧美日韩久久| 91久久福利国产成人精品| 2021久久精品国产99国产精品| 久久久久精品国产亚洲AV无码| 色狠狠久久综合网| 亚洲国产精品综合久久一线|