heap median implementation