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

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box: