This is C# implementation for efficient calculation of the median from a continuous stream of numbers.

One way to do that would be to use two heaps. One max heap on the left and one min heap on the right. In the middle will be our median value. When inserting new value we will make sure that the numbers on the left and on the right are the same. Below is a picture of how it would look:

heaps

And here is the implementation: Source Code in C#

This is a quick implementation of min and max heaps

Share this post:   digg     Stumble Upon     del.icio.us     E-mail

Commenting temporarily disabled