1 import java.util.Comparator;
2
3 public class HeapSort {
4
5 public static <E> void heapSort(E[] list) {
6
7 heapSort(list, (e1, e2) -> ((Comparable<E>)e1).compareTo(e2));
8 }
9
10
11 public static <E> void heapSort(E[] list, Comparator<E> c) {
12
13 Heap<E> heap = new Heap<>(c);
14
15
16 for (int i = 0; i < list.length; i++)
17 heap.add(list[i]);
18
19
20 for (int i = list.length - 1; i >= 0; i--)
21 list[i] = heap.remove();
22 }
23
24
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 }