• <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 閱讀(1572) 評論(0)  編輯 收藏 引用 所屬分類: Data Structures & Algorithms
            By JonsenElizee
            久久综合狠狠综合久久| 久久久久久久综合日本亚洲| 久久国产免费观看精品| 色综合色天天久久婷婷基地| 伊人精品久久久久7777| 久久久一本精品99久久精品66| 品成人欧美大片久久国产欧美| 中文精品久久久久人妻| 99re久久精品国产首页2020| 日日狠狠久久偷偷色综合免费| 日产精品久久久久久久性色| 精品国产综合区久久久久久| 欧美噜噜久久久XXX| 欧美日韩精品久久久久| 色成年激情久久综合| 无码人妻久久久一区二区三区 | 国产精品成人99久久久久 | 亚洲午夜久久久久久噜噜噜| 青青青国产精品国产精品久久久久| 思思久久好好热精品国产| 久久青青草原国产精品免费| 久久AV无码精品人妻糸列| 婷婷久久综合九色综合九七| 亚洲成人精品久久| 精品国产91久久久久久久| 精品国产乱码久久久久久呢| 手机看片久久高清国产日韩| 国产精品久久久天天影视香蕉| 精品久久久久久国产| 久久久久成人精品无码中文字幕 | 日本五月天婷久久网站| 久久国产影院| 久久影视国产亚洲| 久久亚洲色一区二区三区| 精品久久人人做人人爽综合| 国产成人综合久久精品尤物| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 国产精品无码久久久久久| 精品国产青草久久久久福利| 国产一区二区久久久| 久久久久久综合网天天|