Lecture Videos
  1  import java.util.Comparator;
  2  
  3  public class HeapSort {
  4    /** Heap sort method */
  5    public static <E> void heapSort(E[] list) {
  6      // Create a Heap of integers
  7      heapSort(list, (e1, e2) -> ((Comparable<E>)e1).compareTo(e2));
  8    }
  9    
 10    /** Heap sort method */
 11    public static <E> void heapSort(E[] list, Comparator<E> c) {
 12      // Create a Heap of integers
 13      Heap<E> heap = new Heap<>(c);
 14  
 15      // Add elements to the heap
 16      for (int i = 0; i < list.length; i++)
 17        heap.add(list[i]);
 18  
 19      // Remove elements from the heap
 20      for (int i = list.length - 1; i >= 0; i--)
 21        list[i] = heap.remove();
 22    }
 23  
 24    /** A test method */
 25    public static void main(String[] args) {
 26      Integer[] list = {-44, -5, -3, 3, 3, 1, -4, 0, 1, 2, 4, 5, 53}; 
 27      heapSort(list);
 28      for (int i = 0; i < list.length; i++)
 29        System.out.print(list[i] + " ");
 30    }
 31  }