What is the best way to keep track of the median?

Posted by Steven Mou on Programmers See other posts from Programmers or by Steven Mou
Published on 2011-06-28T15:37:40Z Indexed on 2011/06/28 16:30 UTC
Read the original article Hit count: 274

I read a question in one book: Numbers are randomly generated and stored into an (expanding) array, How would you keep track of the median?

There are two data structures can solve the problem. One is the balanced binary tree, the other is two heaps which keep trace of the biggest half and the smallest half of the elements. I think these two solutions has the same running time as O(n lg n), but I am not sure of my judgement.

In your opinions, What is the best way to keep track of the median?

© Programmers or respective owner

Related posts about interview-questions

Related posts about algorithms