public static void main(String[] args) { Scanner in = new Scanner(System.in); double a[] = {9.2, 4.2, 1.1, 6.4, 3.5, 8.9, 2.5, 7.5}; // Array 1 Heap x = new Heap( 100 ); // Array 2 for ( int i = 0; i < a.length; i++ ) { System.out.println("Insert: " + a[i] ); x.put( a[i] ); x.printHeap(); } System.out.println("\n\nKeep removing the first node : "); for ( int i = 0; i < a.length; i++ ) { double r; r = x.remove(1); System.out.print( r + ", "); } System.out.println(); } 
This algorithm uses 2 (two) arrays:




Example:


Input array: a = { 0.0, 9.2, 4.2, 1.1, 6.4, 3.5, 8.9, 2.5, 7.5}; 


// Initially: heap segment = {a[1]} for ( every element x = a[2], a[3], a[4], .... ) { "Include x into the Heap;" // We don't need to do anything !!! // We just pretend the heap segment // has expanded by 1 array element filter x up the heap; } 
NNodes = 1; // Start the Heap with a[1] for (i = 2; i < a.length; i++) { NNodes++; // "Include" the next node HeapFilterUp(a, NNodes); // Filter a[NNodes] up the heap. } 




(Remember that in a Heap, the root node of each subtree must have the smallest value in its subtree 
The filter down algorithm will fix the tree to comply with this property)





repeat these step N times (N = # nodes in the heap) { help = a[1]; // Delete the root node and save the value a[1] = a[last node in heap]; // Replace root node with // fartest right node on lowest level a[empty slot] = help; // Save deleted value in empty slot HeapFilterUp( 1 ); // Fix the heap start at root node } // Optionally, we can reverse the // array elements (because the values are sorted in reverse order) 
for ( i = 1; i < a.length; i++ ) { help = a[1]; // Delete root node a[1] = a[NNodes]; // Replace root node with the last node a[NNodes] = help; // Put the deleted node in the empty slot NNodes; // One less node in Heap HeapFilterDown(a, 1, NNodes); // Fix the Heap } /* ========================================================= This forloop reverse the elements a[1] a[2] ... a[N] ========================================================= */ for ( i = 1, j = a.length1; i < j; i++, j ) { help = a[i]; a[i] = a[j]; a[j] = help; } 
public static void sort( double[] a ) { int i, j, NNodes; double help; /* ======================================= Phase 1 ======================================= */ NNodes = 1; for (i = 2; i < a.length; i++) { NNodes++; // include next node HeapFilterUp(a, NNodes); printHeap(a, NNodes); } /* ======================================= Phase 2 ======================================= */ for ( i = 1; i < a.length; i++ ) { help = a[1]; // Delete root node a[1] = a[NNodes]; // Replace root node with the last node a[NNodes] = help; // Put the deleted node in the empty slot NNodes; // One less node in Heap HeapFilterDown(a, 1, NNodes); // Fix the Heap printHeap(a, NNodes); } /* ======================================= Reverse the (sorted) array ======================================= */ for ( i = 1, j = a.length1; i < j; i++, j ) { help = a[i]; a[i] = a[j]; a[j] = help; } } 
The array a is passed as parameter The parameter k also to tell the method how many nodes there are in the Heap public static void HeapFilterUp( double[] a, int k ) { **** The BODY of the method is UNCHANGED !!!! **** } 
(The algorithm did not change.  that's why I did not include the body)
Only the way that the array is given to the method was changed
This change does not (and should not) affect you understanding of the algorithm !!!)
The array a is passed as parameter The parameter NNodes is also needed to tell the method how many nodes there are in the Heap (k is the index of the delete location) public static void HeapFilterDown( double[] a, int k, int NNodes ) { **** The BODY of the method is UNCHANGED !!!! **** } 
public static void main( String[] args ) { double[] x = {0, 6.4, 2.5, 9.2, 3.5, 8.9, 1.1, 7.5, 4.2} ; System.out.println("Before sort: " + Arrays.toString(x) + "\n" ); HeapSort.sort( x ); // Heap sort System.out.println("\nAfter sort: " + Arrays.toString(x) ); } 
Output:
After sort: [0.0, 1.1, 2.5, 3.5, 4.2, 6.4, 7.5, 8.9, 9.2] 
How to run the program:
