Running Median Using Heap (Java): Our goal is to find median of the elements as they come. So first we should know what is a median. Median: Sort the elements and 1. middle element is the median in case number of elements is odd 2. Or (middle element + (middle + 1)th element)/2 is median Example 1: Input: 10,2,3,4,5 Sort : 2,3,4,5,10 Because number of elements are odd 5/2=2 Example 2: Input: 1,10,2,3,4,5 Sort : 1,2,3,4,5,10 Because number of elements are even, so (n/2th element + n/2+1th element)/2 = (3+4)/2 = 3.5
Programming Bits by: Amit