Skip to content

Instantly share code, notes, and snippets.

@ktraff
Created April 6, 2013 21:35
Show Gist options
  • Save ktraff/5327718 to your computer and use it in GitHub Desktop.
Save ktraff/5327718 to your computer and use it in GitHub Desktop.
maintain the median with heaps 3
public double findMedian(int[] arr)
{
double median = Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++)
{
adjustHeaps(median);
if(arr[i] <= median)
{
maxHeap.add(arr[i]);
}
else minHeap.add(arr[i]);
median = getMedian();
}
return median;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment