• <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
            久久国产免费直播| 久久99精品久久久久久野外| 亚洲精品久久久www| 无遮挡粉嫩小泬久久久久久久 | 久久伊人亚洲AV无码网站| 色综合久久88色综合天天 | 人妻无码αv中文字幕久久琪琪布| 91久久精一区二区三区大全| 欧美一级久久久久久久大| 久久丫精品国产亚洲av不卡 | 久久国内免费视频| 97久久久精品综合88久久| 欧美大战日韩91综合一区婷婷久久青草| 亚洲人成精品久久久久| 99久久精品免费看国产| 亚洲精品乱码久久久久66| 久久久久亚洲AV成人网人人软件| 伊人久久大香线蕉av不卡| 精品国产青草久久久久福利| 无码伊人66久久大杳蕉网站谷歌 | 亚洲综合久久夜AV | 久久福利青草精品资源站| 中文字幕久久波多野结衣av| 久久精品无码专区免费| 97久久精品国产精品青草| 亚洲AV无码一区东京热久久 | 久久国产精品成人免费| 99久久精品国产一区二区 | 久久久噜噜噜久久| 99久久亚洲综合精品成人| 久久精品国产福利国产秒| 国产精品免费福利久久| 久久精品水蜜桃av综合天堂| 18岁日韩内射颜射午夜久久成人| 超级碰碰碰碰97久久久久| 亚洲乱码日产精品a级毛片久久 | 久久精品国产亚洲沈樵| 国内精品久久久久| 91精品国产综合久久香蕉| 精品国产热久久久福利| 久久精品中文字幕第23页|