[Google面试]10G的数据如何用2G的空间找到中位数(Median)

Stack Overflow上面的解答,简直是赞:
Create an 8-byte array with 2^16 entries. Take your input numbers, shift off the bottom sixteen bits, and create a histogram. (That said, each bin is the count of that number)
Now you count up in that histogram until you reach the bin that covers the midpoint of the values.
Pass through again, ignoring all numbers that don’t have that same set of top bits, and make a histogram of the bottom bits.
Count up through that histogram until you reach the bin that covers the midpoint of the (entire list of) values.
Now you know the median, in O(n) time and O(1) space (in practice, under 1 MB).

posted on 2015-03-29 12:57  Seth_L  阅读(123)  评论(0编辑  收藏  举报