TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Median-of-Medians Algorithm

115 pointsby MidsizeBlowfishover 11 years ago

12 comments

susi22over 11 years ago
Note that you shouldn&#x27;t use the algorithm in practice (unless you need certain guarantees) since the average performance isn&#x27;t that great. This is just like Mergesort vs Quicksort where Mergesort guarantees O(NlogN) yet Quicksort is usually much faster. The constant for the number of comparisons is quite high. Compare this to the <i>average</i> complexity for Quickselect with these pivot strategies:<p>- Random pivot (ie Median of 1) has 3.39N comparisons<p>- Median of 3 has 2.75N<p>If you want the fastest possible algorithm you should follow<p><a href="http://dx.doi.org/10.1109/TSP.2012.2197394" rel="nofollow">http:&#x2F;&#x2F;dx.doi.org&#x2F;10.1109&#x2F;TSP.2012.2197394</a><p>which proposes to choose the median of N^{2&#x2F;3}<i>1&#x2F;(4</i>pi)^{1&#x2F;3} elements as the pivot and then also uses a different strategy to choose the second pivot (close to the median yet &quot;safe&quot;).<p>Another fast algorithm is Floyd &amp; Rivest&#x27;s SELECT (yes the same crypto Rivest) which is also asymptotically optimal and implementations exist. (Paper title: &quot;Expected time bounds for selection&quot;)
评论 #6629287 未加载
vowellessover 11 years ago
This is one of my favorite algorithms. I use it frequently in my code (example: filtering noise in a real time system) and get giddy when I talk to others about it. One of the contributors to this algorithm was Manuel Blum [0], whose contributions (and his students&#x27;) touch pretty much every corner of Computer Science.<p>His lecture notes from his course at Carnegie Mellon describe this algorithm quite clearly [1].<p>[0] <a href="http://en.wikipedia.org/wiki/Manuel_Blum" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Manuel_Blum</a><p>[1] <a href="http://www.cs.cmu.edu/afs/cs/academic/class/15451-s07/www/lecture_notes/lect0125.pdf" rel="nofollow">http:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;afs&#x2F;cs&#x2F;academic&#x2F;class&#x2F;15451-s07&#x2F;www&#x2F;le...</a>
评论 #6631199 未加载
awoolf27over 11 years ago
According to the man himself, this algorithm was found by Manuel Blum when trying to prove that it was impossible to do k selection in linear time deterministically
评论 #6629423 未加载
theszover 11 years ago
This is so trivial!<p>Actually, if you use lazy data structures, then sorting the list lazily and taking i-th element will give you the best possible complexity: <a href="http://en.wikipedia.org/wiki/Selection_algorithm#Language_support" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Selection_algorithm#Language_su...</a><p>In Haskell, it is nothing more than &quot;ithSmallest xs i = sort xs !! i&quot;.
评论 #6630710 未加载
评论 #6630382 未加载
评论 #6630686 未加载
wfunctionover 11 years ago
If you like this, go look into the suffix array construction algorithm, called DC3 (&quot;difference cover, modulo 3&quot;). It&#x27;s even more mindblowing.
评论 #6628983 未加载
jmlundbergover 11 years ago
The C++ standard library method std::nth_element states complexity; linear time on average. ( <a href="http://en.cppreference.com/w/cpp/algorithm/nth_element" rel="nofollow">http:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;algorithm&#x2F;nth_element</a> )<p>I wonder if there are other possible implementations?
crb002over 11 years ago
I&#x27;m coding it in Ruby. Check back tomorrow and I should have it finished. <a href="https://gist.github.com/chadbrewbaker/7202412#file-median-rb" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;chadbrewbaker&#x2F;7202412#file-median-rb</a>
评论 #6629351 未加载
评论 #6637554 未加载
NotSoNormalover 11 years ago
Nice writeup. There&#x27;s a good source already though: <a href="http://www.cs.cmu.edu/~avrim/451f11/lectures/lect0908.pdf" rel="nofollow">http:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~avrim&#x2F;451f11&#x2F;lectures&#x2F;lect0908.pdf</a>
myleover 11 years ago
You can use the same idea in quicksort while choosing the pivot (and make it O(n log n)) but you don&#x27;t because it is expensive.<p>There is a very nice and simple probabilistic algorithm instead.
newobjover 11 years ago
So embarrassing when a candidate busted this out on me in an interview - but couldn&#x27;t quite explain it - so I was convinced he was wrong &#x2F; out of his mind. Later I looked it up in the CLR and sure enough...
curiousDogover 11 years ago
Well written. This was the first algo we were taught in our algorithms design course from CLRS. Blew my mind at that time.
felipecruzover 11 years ago
very cool algo!! a C impl: <a href="https://github.com/felipecruz/paa/blob/master/t1/knth_n_mom.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;felipecruz&#x2F;paa&#x2F;blob&#x2F;master&#x2F;t1&#x2F;knth_n_mom....</a>