Name Best-case Average-case Worst-case Memory Stable
HeapSort n log n n log n n log n 1 no

HeapSort


Your browser does not support the HTML5 canvas tag.

Java Code

public void heapSort(int[] array) {
     for (int i = (array.length - 2) / 2; i >= 0; i--)
          heapify( array , i , array.length - 1 );

     for (int i = array.length - 1; i > 0; i--) {
          swapKeys( array , 0 , i );
          heapify(array , 0 , i - 1 );
     }
}
private void heapify(int[] array, int i, int m) {
     int j;
     while ( 2 * i + 1 <= m ) {
          j = 2 * i + 1;
          if ( j < m ) {
               if ( array[ j ] < array[ j + 1 ] )
                    j++;
          }
          if (array[ i ] < array[ j ]) {
               swapKeys( array , i , j );
               i = j;
          } else
               i = m;
     }
}

public void swapKeys(int[] array, int i, int j) {
     int temp;
     temp = array[ i ];
     array[ i ] = array[ j ];
     array[ j ] = temp;
} 
            

How to use

Use the textfield to type in a number and add it by either pressing ENTER or by clicking on the "Add" button.
You can also add 10 random numbers at once by clicking on the "10 Random Keys" button. Overall you can add up to 63 keys.
The "Sort" button starts to sort the keys with the selected algorithm.