• <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 閱讀(275) 評論(0)  編輯 收藏 引用 所屬分類: Data structure and algorithms

            区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 国产三级久久久精品麻豆三级| 国内精品伊人久久久久影院对白 | 少妇被又大又粗又爽毛片久久黑人| 国产AV影片久久久久久| 亚洲国产精品成人久久蜜臀| 女人高潮久久久叫人喷水| 精品国产乱码久久久久久呢| 久久久国产精品亚洲一区| 国产精品gz久久久| 久久婷婷色香五月综合激情| 久久综合给合久久国产免费| 欧美久久综合九色综合| 久久精品www人人爽人人| 999久久久免费国产精品播放| 欧美精品久久久久久久自慰| 久久99精品国产麻豆不卡| 日韩AV无码久久一区二区 | 亚洲国产另类久久久精品小说| 四虎影视久久久免费| 狠狠88综合久久久久综合网| 伊人久久五月天| 人妻无码久久精品| 国产精品无码久久综合网| 狠狠色婷婷综合天天久久丁香 | 伊人伊成久久人综合网777| 国产精品久久永久免费| 精品久久久久久无码中文字幕一区| 无码国内精品久久人妻麻豆按摩 | 一本久久久久久久| 99久久无码一区人妻a黑| 久久久久99精品成人片欧美| 久久精品国产免费观看三人同眠| 精品无码人妻久久久久久| 日韩精品久久久久久| 久久精品国产第一区二区| 亚洲乱码日产精品a级毛片久久| 久久夜色精品国产亚洲| 久久99免费视频| 国产免费久久精品丫丫| 久久精品国产福利国产琪琪|