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

            JonsenElizee

            Software Developing Blog

            "An idea is fragile . It can be killed by a scornful smile or a yawn .It can be mound down by irony and scared to death by a cold look."
            "Most cultures throughout human history have not liked creative individuals .They ignore them or kill them.It is a very efficient way of stopping creativity."

            ------Advertising boss Charles Browe and Howard Gardner ,professor at Harvard

               :: 首頁 :: 新隨筆 ::  ::  :: 管理 ::
            Issue:
            內存只能容納100W個數,
            現在有1億數,混序,
            然后求這一億個數的中位數,
            中位數(就是一個有序數組的中間數字,數組長度為偶數就是兩個,奇數則只有一個)

            /*****************************************************************************/
            /* this program should find out the median(s) in N(logN)/(logB) no matter there
            /* is duplicated data or not.
            /*
            /* with memory limitation: 4MB memory available. in fact, it can work on 1MB.
            /* I would like to express the idea with code.
            /* here is just the pseudocode with out any optimization for easy understanding.
            /* 
            /* maybe, there is no need to check sum == N/2 or N/2+1, but this will get the 
            /* median(s) as soon as possible with out more recursive invoking.
            /*
            /* sorry for any logical problem. please let me know if you found any.
            /****************************************************************************
            */


            int N = 100000000;// total data number is N
            int B = 1000000;  // number of buckets.
            int min, max;
            scan data from 
            0 to N-1 get min and max; // O(N);

            // num the data number that is less than min
            // min the minimum data in the target data range to find median(s)
            // max the maximum data in the target data range to find median(s)
            void find_median(int num=0int min, int max)
            {
                
            if(min == max) {
                    
            if(N%2) {
                        print medians are min and max;
                    }
                    
            else print median is min; // show you the result
                    return// exit
                }

                
            // count all the data in O(N)
                
            int m = max-min > B ? ((max-min)%B ? (max-min)/B + 1 : (max-min)/B)1;
                
            int cnt[B] = {0}; // count the data read.
                while(!EOF) {
                    
            int data = read_data()-min;
                    // count data in this range [min, max]
                    // data/m will get the position of data to increase the cnt[?] value
                    
            if(data >= min && data <= max) cnt[data/m]++;
                }
                
                
            int sum = num, median_1, median_2, i = 0;
                
            while(sum < N/2) sum += cnt[i++];
                i
            --// median is in the range of [i*m, (i+1)*m-1]

                
            /* get median(s) when sum is N/2 */
                
            if(sum == N/2) {
                    
            if(N%2) { // N is even and there are two medians.
                        median_1 = (i+2)*m-1;
                        median_2 
            = i*m;
                        
            while(!EOF) {
                            
            int data = read_data()-min;
                            
            if(data/== i) {
                                median_2 
            = (data > median_2 ? data : median_2);
                            }
                            
            if(data/== i+1) {
                                median_1 
            = (data < median_1 ? data : median_1);
                            }
                        }
                        pintf medians are median_1 and median_2;
                        
            return;
                    }
               
                   
            else { // N is an odd number and there is one median.
                        median_1 = (i+2)*m-1;
                       
            while(!EOF) {
                           
            int data = read_data();
                           
            if(data/== i+1) median_1 = (data < median_1 ? data : median_1);
                        }
                        print median 
            is median_1;
                       
            return;
                    }
                }

                
            /* get median(s) when sum is N/2+1 */
                
            if(sum == N/2+1) {
                    
            if(N%2) { // N is even and there are two medians.
                        median_1 = i*m;
                        median_2 
            = i*m;
                        
            while(!EOF) {
                            
            int data = read_data();
                            
            if(data/== i) {
                                median_1 
            = max;
                                median_2 
            = (data > median_2 ? data : median_2);
                            }
                        }
                        pintf medians are median_1 and median_2;
                        
            return;
                    }
               
                   
            else {
                        median_2 
            = i*m; // get (N/2+1)th data.
                        while(!EOF) {
                           
            int data = read_data();
                           
            if(data/== i) median_2 = (data > median_2 ? data : median_2);
                        }
                        print median 
            is median_2;
                       
            return;
                    }
                }

               
                
            // if sum is not N/2 or (N/2 + 1), recursively to find out the median(s)
                min 
            = (i+1)*m-1;
                max 
            = i*m;
                
            // get min and max for next processing
                while(!EOF) 
                {
                    
            int data = read_data()-min;
                    
            if(data/== i)
                    {
                        min 
            = (data < min ? data : min);
                        max 
            = (data > max ? data : max);
                    }
                }
                find_median(sum, min, max);
            }


            posted on 2010-10-28 16:39 JonsenElizee 閱讀(1587) 評論(0)  編輯 收藏 引用 所屬分類: Data Structures & Algorithms
            By JonsenElizee
            亚洲AV无码成人网站久久精品大| 日本精品久久久久中文字幕8| 偷窥少妇久久久久久久久| 思思久久99热只有频精品66| 欧美亚洲国产精品久久高清| 国内精品久久久久影院一蜜桃| 色综合久久天天综合| 久久受www免费人成_看片中文| AV无码久久久久不卡蜜桃| 93精91精品国产综合久久香蕉 | 久久99久久成人免费播放| 亚洲午夜久久久| 久久99久久99小草精品免视看 | 欧美喷潮久久久XXXXx| 久久青青草原精品影院| 伊人久久精品影院| 91亚洲国产成人久久精品网址 | 亚洲午夜久久久| 99久久精品九九亚洲精品| 国产精品久久久久久久久软件| 久久本道伊人久久| 国产毛片欧美毛片久久久| 国产99久久久国产精免费| 亚洲婷婷国产精品电影人久久 | 2021久久精品免费观看| 国产精品女同一区二区久久| 久久国产热精品波多野结衣AV| 日韩精品无码久久一区二区三| 久久99热国产这有精品| 狠狠色丁香久久婷婷综合五月| 久久精品国产欧美日韩99热| 国产激情久久久久影院老熟女免费 | 91久久福利国产成人精品| 97久久超碰国产精品2021| 色欲久久久天天天综合网精品| 亚洲欧美一区二区三区久久| 欧美久久综合九色综合| 色99久久久久高潮综合影院| 久久国产热这里只有精品| 国产一区二区精品久久凹凸| 国产精品成人99久久久久|