惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
博客园 - 叶小钗
AI
AI
T
Tor Project blog
Forbes - Security
Forbes - Security
W
WeLiveSecurity
博客园_首页
爱范儿
爱范儿
J
Java Code Geeks
B
Blog
G
GRAHAM CLULEY
aimingoo的专栏
aimingoo的专栏
Cloudbric
Cloudbric
C
CXSECURITY Database RSS Feed - CXSecurity.com
TaoSecurity Blog
TaoSecurity Blog
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
有赞技术团队
有赞技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
H
Help Net Security
博客园 - 三生石上(FineUI控件)
C
Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 司徒正美
The Last Watchdog
The Last Watchdog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
S
Secure Thoughts
Spread Privacy
Spread Privacy
F
Fortinet All Blogs
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Security Latest
Security Latest
Webroot Blog
Webroot Blog
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog

博客园 - Zero Lee

调用栈(call stack) 关于STL allocator Calculate maximum sum of any subarray set Calcuate power n of x recursively Convert one binary search tree to double-linked list 设计包含min函数的栈 类模板的模板友元函数定义 一道百度的面试题解答 非printf形式的十六进制和二进制打印(雅虎面试题) 一道腾讯面试题 (转)C++中extern “C”含义深层探索 删除与某个字符相邻且相同的字符 产生全排列的方法解析 一组数的全排列和组合程序实现 求一个正整数的平方根程序实现 [转]多线程队列的算法优化 [转载] STL allocator的介绍和一个基于malloc/free的allocator的简单实现 如何将一片内存链接成链表 One simple counted object pointer
selection algorithm to select nth small elements based on partition
Zero Lee · 2012-06-17 · via 博客园 - Zero Lee

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 Example

 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