Tag Archives: heap

Find the median in an array, by using heap

This is a practice for heap. The problem is from this blog: link check my recent code on github by PriorityQueue: link package feb; public class FindMedian {     /*      * Return the median of arr[]. It uses a max heap, min heap. Keep the number      * of elements in 2 heaps differ… Read More »

Find the median in an array

Problem: If a random array is [2, 20, 40, 60, 50, 65, 25, 10, 30, 35,  52, 63, 67, 3], find the median of the array. 1. For each new Element in array, if it is less than the root of max heap, put it in the max heap; or put it in the min heap.… Read More »

Find TopK value, by small heap

This is the code to get top k values from a input. /*  * Get the Top Largest K values from small heap  */ public class TopK {     public static void main(String[] args) {         int[] array = {5, 13,10,12,15,11, 8,23, 40, 16, 83, 23, 31, 73, 59, 25, 75};         findTopK(array,… Read More »