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

            Zero Lee的專欄

            selection algorithm to select nth small elements based on partition

            http://en.wikipedia.org/wiki/Selection_algorithm
            1. partition algorithm
             1  function partition(list, left, right, pivotIndex)
             2      pivotValue := list[pivotIndex]
             3      swap list[pivotIndex] and list[right]  // Move pivot to end
             4      storeIndex := left
             5      for i from left to right-1
             6          if list[i] < pivotValue
             7              swap list[storeIndex] and list[i]
             8              storeIndex := storeIndex + 1
             9      swap list[right] and list[storeIndex]  // Move pivot to its final place
            10      return storeIndex

            2. selection algorithm
             1 function select(list, left, right, k)
             2      loop
             3          select pivotIndex between left and right
             4          pivotNewIndex := partition(list, left, right, pivotIndex)
             5          if k = pivotNewIndex - left + 1
             6              return list[pivotNewIndex]
             7          else if k < pivotNewIndex left + 1
             8              right := pivotNewIndex-1
             9          else
            10              left := pivotNewIndex+1
            11              k := k pivotNewIndex    

            >> In above last statement, one bug. Should be
               k := k-pivotNewIndex-left+1
               left := pivotNewIndex+1

            3. code
             1 int partition(std::vector<int>& a, int l, int r)
             2 {
             3     int i = l, j = l;
             4     int p = a[r];
             5     while (j<r) {
             6         if (a[j] < p) {
             7             std::swap(a[i], a[j]);
             8             i++;
             9         }
            10         j++;
            11     }
            12     std::swap(a[r], a[i]);
            13     return i;
            14 }
            15 
            16 void select_kth_smallest(std::vector<int>& a, int l, int r, int k)
            17 {
            18     while (1) {
            19         int cut = partition(a, l, r);
            20 #if 0
            21         std::copy(a.begin()+l, a.begin()+cut, std::ostream_iterator<int>(std::cout, " "));
            22         std::cout << " <=" << a[cut] << "=> ";
            23         std::copy(a.begin()+cut+1, a.begin()+r+1, std::ostream_iterator<int>(std::cout, " "));
            24         std::cout << "cut("<<cut<<"),k("<<k<<"),l("<<l<<"),r("<<r<<")\n";
            25 #endif
            26         if (cut-l+1==k)
            27             break;
            28         else if (k < cut-l+1)
            29             r = cut-1;
            30         else {
            31             k = k-cut-1+l;
            32             l = cut+1;
            33         }
            34     }
            35 }
            36 

            posted on 2011-03-17 20:20 Zero Lee 閱讀(276) 評論(0)  編輯 收藏 引用 所屬分類: Data structure and algorithms

            欧美综合天天夜夜久久| 一本一本久久A久久综合精品 | 久久久精品国产sm调教网站| 亚洲人成网亚洲欧洲无码久久 | 久久人人爽人人爽AV片| 2020久久精品亚洲热综合一本 | 欧美精品一区二区久久| 久久精品无码专区免费青青| 久久91精品综合国产首页| 婷婷五月深深久久精品| 久久久亚洲精品蜜桃臀| 无码人妻久久一区二区三区免费丨| 日韩av无码久久精品免费| 久久99精品久久久久久齐齐| 色8久久人人97超碰香蕉987| 久久久精品人妻无码专区不卡| 亚洲中文字幕无码久久2017| 久久久精品波多野结衣| 青青青伊人色综合久久| 97久久超碰成人精品网站| 久久人人爽人人爽人人爽| 精品人妻伦九区久久AAA片69 | 久久夜色精品国产欧美乱| 一本色道久久综合狠狠躁篇 | 亚洲国产精品无码久久青草| 久久福利青草精品资源站| 亚洲中文字幕无码久久综合网| 久久精品无码免费不卡| 久久久久久亚洲精品不卡| 久久中文字幕一区二区| 久久久久四虎国产精品| 97久久精品人妻人人搡人人玩| 国产精品视频久久久| av无码久久久久久不卡网站| 2021精品国产综合久久| 久久综合亚洲欧美成人| 久久国产亚洲精品无码| 国产一区二区三区久久| 中文精品久久久久国产网址| 91久久精品国产成人久久| 久久本道久久综合伊人|