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

            統計

            • 隨筆 - 50
            • 文章 - 42
            • 評論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 164627
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

            尋找k大

             自己寫的尋找數組中第k大元素的函數,采用二分查找法,平均時間復雜度O(logN),有更好的方法歡迎指正

             感謝 雙杯獻酒 同學指正,我重新思考了一下次算法的時間復雜度
            理想情況下如果每次都正好分到中間的話,每次比較的次數就是公比為0.5的等比數列,也就得到
            T(N)=a1(1-q^n)/(1-q) =N(1-0.5^(logN))/0.5=2N-0,5^(logN-1)
            當N→∞時,得平均時間復雜度為O(N)
            但是本算法的最壞情況確實O(N^2)

            /************************************************************************/
            /* find the k bigger number in the array, using Bisection method
            /* Contact:lyi.tech@gmail.com
            /* 2011-3-10
            /***********************************************************************
            */

            int findKBiggest(int *array,int begin,int end,int k);
            int nonrecursive_FindKBiggest(int *array,int length,int k);
            void switch2(int *a,int *b);
            //************************************
            // Method:    findKBiggest
            // FullName:  findKBiggest
            // Access:    public 
            // Returns:   int
            // Qualifier:
            // Parameter: int * array
            // Parameter: int length
            // Parameter: int k
            // Parameter: int * k_value 
            //                the K biggest number,the biggest number is labeled by 1
            //************************************
            int findKBiggest(int *array,int length,int k,int *k_value)
            {
                
            if (k>length||k<1||length<1)
                    
            return -1;
                
            else 
                
            {
                    
            //*k_value=findKBiggest(array,0,length-1,k);
                    *k_value=nonrecursive_FindKBiggest(array,length,k);
                    
            return 0;
                }

            }


            //************************************
            // Method:    recursive method
            // FullName:  findKBiggest
            // Access:    public 
            // Returns:   the K biggest number,the biggest number is labeled by 1
            // Qualifier:
            // Parameter: int * array
            // Parameter: int begin
            // Parameter: int end
            // Parameter: int k base on 1
            //************************************
            int findKBiggest(int *array,int begin,int end,int k)
            {
                
            int tmpbegin=begin;
                
            int tmpend=end;
                
            int *tmp=array+begin++;
                
            int length=end-begin+1;
                
            while (1)
                
            {
                    
            while ((*(array+begin) >= *tmp)&&(begin<tmpend))
                        begin
            ++;
                    
            while ((*(array+end) < *tmp)&&(end>tmpbegin))
                        end
            --;
                    
            if (begin<end)
                    
            {
                        switch2(array
            +begin,array+end);
                    }

                    
            else
                    
            {
                        switch2(tmp,array
            +end);
                        
            break;
                    }

                }

                
            if (end+1==k)
                    
            return *tmp;
                
            else if(end+1<k)
                    
            return findKBiggest(array,end+1,tmpend,k);
                
            else
                    
            return findKBiggest(array,tmpbegin,end-1,k);

            }


            void switch2(int *a,int *b)
            {
                
            if (a!=b)
                
            {
                    
            *a=*a+*b;
                    
            *b=*a-*b;
                    
            *a=*a-*b;
                }

            }


            //************************************
            // Method:    nonrecursive method
            // FullName:  nonrecursive_FindKBiggest
            // Access:    public 
            // Returns:   the K biggest number,the biggest number is labeled by 1
            // Qualifier:
            // Parameter: int * array
            // Parameter: int length
            // Parameter: int k
            //************************************
            int nonrecursive_FindKBiggest(int *array,int length,int k)
            {
                
            int begin=0,end=length-1;
                
            int tmpbegin,tmpend;
                
            int posnow=0;
                
            while (1)
                
            {    
                    tmpbegin
            =begin+1;
                    tmpend
            =end;
                    
            while (1)
                    
            {
                        
            while (*(array+tmpbegin) >= *(array+begin) && tmpbegin<end)
                            tmpbegin
            ++;
                        
            while (*(array+tmpend) < *(array+begin) && begin < tmpend)
                            tmpend
            --;
                        
            if (tmpbegin<tmpend)
                        
            {
                            switch2(array
            +tmpbegin,array+tmpend);
                        }

                        
            else
                        
            {
                            switch2(array
            +begin,array+tmpend);
                            
            break;
                        }

                    }

                    
            if (k==tmpend+1)
                        
            break;
                    
            else if(k>tmpend+1)
                        begin
            =tmpend+1;
                    
            else
                        end
            =tmpend-1;
                }

                
            return *(array+tmpend);
            }

             

             

             

             

            posted on 2011-03-10 12:38 pear_li 閱讀(1958) 評論(10)  編輯 收藏 引用 所屬分類: Algorithm

            評論

            # re: 尋找k大 2011-03-10 14:03 gbb21

            Please be attention to your ugly Chiglish naming in the code.
              回復  更多評論    

            # re: 尋找k大[未登錄] 2011-03-10 14:23 pear_li

            @gbb21
            see it or not,then shut fu_cking up
              回復  更多評論    

            # re: 尋找k大[未登錄] 2011-03-10 14:43 R

            BFPRT.
              回復  更多評論    

            # re: 尋找k大 2011-03-10 17:51 hook

            樓上說的沒錯,請參考:
            http://en.wikipedia.org/wiki/Selection_algorithm
              回復  更多評論    

            # re: 尋找k大[未登錄] 2011-03-10 22:37 pear_li

            @hook
            貌似和我算法很相似
              回復  更多評論    

            # re: 尋找k大[未登錄] 2011-03-11 09:14 vincent

            怎么得出的logN的復雜度。。
              回復  更多評論    

            # re: 尋找k大 2011-03-11 11:04 雙杯獻酒

            對于數據Data[N]
            如果已經排序,則第k大的元素就是Data[k],何須查找?復雜度O(1)
            如果尚未排序,就不能使用二分查找.
            如果要先排序, 通用的基于比較排序最低復雜度也是O(N*logN)
            最經典的BFPRT算法復雜度也是O(N), 直觀來說,要確定第k大的元素,每個數據總要過一遍吧. 那復雜度也至少是O(N), 要在O(logN)找到是不可能的.
            不明白作者怎么弄的.
              回復  更多評論    

            # re: 尋找k大 2011-03-11 21:46 pear_li

            @雙杯獻酒
            說的不錯
              回復  更多評論    

            # re: 尋找k大 2011-03-12 15:05 Devil Wang

            有O(logN)的算法 。如果你看過算法 導論 關于 快排那部分的算法的話。

            你要做的就是改造partition算法。
              回復  更多評論    

            # re: 尋找k大[未登錄] 2011-03-13 18:28 pear_li

            @Devil Wang
            這個真沒有,如果有還請明示,那本書我看過。。。
              回復  更多評論    
            久久久久久国产精品免费免费| 久久精品无码专区免费青青| 亚洲国产精品久久久久婷婷老年| 久久精品国产一区二区三区日韩| 国产激情久久久久影院小草| 久久亚洲AV成人无码国产| 99久久超碰中文字幕伊人 | 久久久WWW成人| 一级女性全黄久久生活片免费 | 一本久道久久综合狠狠爱| 国内精品久久人妻互换| 久久精品无码一区二区三区免费| 色播久久人人爽人人爽人人片AV| 日本免费久久久久久久网站| 伊人久久大香线蕉精品不卡 | 97久久婷婷五月综合色d啪蜜芽 | 国产激情久久久久影院老熟女| 亚洲中文字幕无码久久精品1| 色噜噜狠狠先锋影音久久| 欧洲精品久久久av无码电影| 久久亚洲电影| 久久99国产一区二区三区| 国内精品久久久久| 久久久久亚洲AV无码永不| 久久午夜福利无码1000合集| 四虎影视久久久免费观看| 国产精品久久久天天影视香蕉 | 久久香蕉国产线看观看精品yw| 久久99精品久久久久久野外| 色成年激情久久综合| 一本久久a久久精品综合夜夜| 久久er99热精品一区二区| 热re99久久精品国99热| 亚洲AV无码久久| 久久婷婷国产综合精品 | 久久人妻少妇嫩草AV无码专区| 人妻无码精品久久亚瑟影视| 色欲综合久久躁天天躁| 久久综合九色综合久99| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 午夜久久久久久禁播电影|