One of my friend has been asked with a question
Retrieving the max top 100 numbers from one hundred million of numbers
in a rece
@darius can actually be improved !!!
By "pruning" or deferring the heap-replace operation as required
Suppose we have a=1000 at the top of the heap
It has c,b siblings
We know that c,b>1000
a=1000
+-----|-----+
b>a c>a
We now read the next number x=1035
Since x>a we should discard a.
Instead we store (x=1035, a=1000) at the root
We do not (yet) bubble down the new value of 1035
Note that we still know that b,cx
Now, we get the next number y
when yx>a then we replace x with y (the root now has (y, a=1000))
=> we saved log(m) steps here, since x will never have to bubble down
when a>y>x then we need to bubble down y recursively as required
Worst run time is still O(n log m)
But average run time i think might be O(n log log m) or something
In any case, it is obviously a faster implementation