• <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
            久久亚洲国产精品五月天婷| 国内精品久久久久久99| 精品久久久久久久久午夜福利| 久久久精品人妻无码专区不卡| 亚洲七七久久精品中文国产| 超级97碰碰碰碰久久久久最新| 亚洲国产美女精品久久久久∴| 久久精品国产亚洲AV无码偷窥| 99久久精品免费国产大片| 亚洲国产成人精品91久久久| 欧美大香线蕉线伊人久久| 久久93精品国产91久久综合| 影音先锋女人AV鲁色资源网久久 | 国产高潮国产高潮久久久| 亚洲欧美日韩中文久久| 久久久久99精品成人片直播| 国产精品天天影视久久综合网| 久久九九久精品国产免费直播| 久久国产精品免费一区| 久久综合五月丁香久久激情| 久久亚洲精品成人无码网站| 无码伊人66久久大杳蕉网站谷歌 | 日韩美女18网站久久精品| 久久婷婷是五月综合色狠狠| 高清免费久久午夜精品| 久久无码av三级| 亚洲色大成网站www久久九| 久久夜色精品国产噜噜麻豆| 久久亚洲精品视频| 久久成人国产精品免费软件| 久久国产视频网| 午夜福利91久久福利| 99久久婷婷国产综合亚洲| 久久亚洲国产成人精品无码区| 久久人妻少妇嫩草AV蜜桃| 久久久久久久99精品免费观看| 久久无码一区二区三区少妇| 久久精品99久久香蕉国产色戒 | 国产99精品久久| 久久久精品视频免费观看| 国产午夜精品久久久久免费视|