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

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

            /*****************************************************************************/
            /* 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 閱讀(1571) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): Data Structures & Algorithms
            By JonsenElizee
            av国内精品久久久久影院| 久久久久国产一区二区| 久久综合伊人77777麻豆| 亚洲精品无码久久久久AV麻豆| 久久久WWW成人免费毛片| 久久99精品国产麻豆宅宅| 久久精品国产99久久无毒不卡| Xx性欧美肥妇精品久久久久久| 欧美午夜A∨大片久久| 久久久婷婷五月亚洲97号色| 国产精自产拍久久久久久蜜| 午夜精品久久久久久久久| 99精品久久久久久久婷婷| 2021最新久久久视精品爱| 精品久久久久久亚洲精品| 亚洲日韩欧美一区久久久久我| 久久超乳爆乳中文字幕| 中文成人无码精品久久久不卡 | 久久亚洲AV无码精品色午夜 | 国产精品久久久久国产A级| 久久无码人妻精品一区二区三区| 狠狠色噜噜色狠狠狠综合久久| 亚洲国产精久久久久久久| 久久精品免费一区二区| 久久激情亚洲精品无码?V| 99国产欧美久久久精品蜜芽| 国产色综合久久无码有码| 精品国产综合区久久久久久| 2021少妇久久久久久久久久| 人妻无码αv中文字幕久久 | 久久精品国产99国产精品澳门| 波多野结衣久久精品| 久久亚洲av无码精品浪潮| 久久九九久精品国产| 久久成人精品| 久久精品国产亚洲一区二区三区| 日本免费久久久久久久网站| 99精品久久久久中文字幕| 丁香狠狠色婷婷久久综合| 久久99精品久久久久久久不卡| 无码超乳爆乳中文字幕久久|