Note that there are some explanatory texts on larger screens.

plurals
  1. POKeeping largest 10 numbers
    primarykey
    data
    text
    <p>While iterating through a dataset, what's the best way to keep track of only the top 10 numbers so far, in sorted order?</p> <p>Solution...Ended up implementing Generic Min and Max Heaps...as sadly they are not available in Java libraries or readily on the internet....No gauruntees on the code...</p> <pre><code>import java.util.ArrayList; public class MaxHeapGeneric &lt;K extends Comparable&gt; { //ArrayList to hold the heap ArrayList&lt;K&gt; h = new ArrayList&lt;K&gt;(); public MaxHeapGeneric() { } public int getSize() { return h.size(); } private K get(int key){ return h.get(key); } public void add(K key){ h.add(null); int k = h.size() - 1; while (k &gt; 0){ int parent = (k-1)/2; K parentValue = h.get(parent); //MaxHeap - //for minheap - if(key &gt; parentValue) if(key.compareTo(parentValue) &lt;= 0) break; h.set(k, parentValue); k = parent; } h.set(k, key); } public K getMax() { return h.get(0); } public void percolateUp(int k, K key){ if(h.isEmpty()) return ; while(k &lt; h.size() /2){ int child = 2*k + 1; //left child if( child &lt; h.size() -1 &amp;&amp; (h.get(child).compareTo(h.get(child+1)) &lt; 0) ) { child++; } if(key.compareTo(h.get(child)) &gt;=0) break; h.set(k, h.get(child)); k = child; } h.set(k, key); } public K remove() { K removeNode = h.get(0); K lastNode = h.remove(h.size() - 1); percolateUp(0, lastNode); return removeNode; } public boolean isEmpty() { return h.isEmpty(); } public static void main(String[] args) { MaxHeapGeneric&lt;Integer&gt; test = new MaxHeapGeneric&lt;Integer&gt;(); test.add(5); test.add(9); test.add(445); test.add(1); test.add(534); test.add(23); while(!test.isEmpty()) { System.out.println(test.remove()); } } } </code></pre> <p>And a min heap</p> <pre><code>import java.util.ArrayList; public class MinHeapGeneric &lt;K extends Comparable&gt; { //ArrayList to hold the heap ArrayList&lt;K&gt; h = new ArrayList&lt;K&gt;(); public MinHeapGeneric() { } public int getSize() { return h.size(); } private K get(int key){ return h.get(key); } public void add(K key){ h.add(null); int k = h.size() - 1; while (k &gt; 0){ int parent = (k-1)/2; K parentValue = h.get(parent); //for minheap - if(key &gt; parentValue) if(key.compareTo(parentValue) &gt; 0) break; h.set(k, parentValue); k = parent; } h.set(k, key); } public K getMax() { return h.get(0); } public void percolateUp(int k, K key){ if(h.isEmpty()) return ; while(k &lt; h.size() /2){ int child = 2*k + 1; //left child if( child &lt; h.size() -1 &amp;&amp; (h.get(child).compareTo(h.get(child+1)) &gt;= 0) ) { child++; } if(key.compareTo(h.get(child)) &lt; 0) break; h.set(k, h.get(child)); k = child; } h.set(k, key); } public K remove() { K removeNode = h.get(0); K lastNode = h.remove(h.size() - 1); percolateUp(0, lastNode); return removeNode; } public boolean isEmpty() { return h.isEmpty(); } public static void main(String[] args) { MinHeapGeneric&lt;Integer&gt; test = new MinHeapGeneric&lt;Integer&gt;(); test.add(5); test.add(9); test.add(445); test.add(1); test.add(534); test.add(23); while(!test.isEmpty()) { System.out.println(test.remove()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload