提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
实现堆
import static java.lang.System.out;//来 自 nowjava - 时代Java public class Heap { private static void sink (Comparable[] pq, int k, int N) { while (2 * k <= N) { int j = 2 * k; if (j < N && less(pq, j, j+1)) j++; if (!less(pq, k, j)) break; exch(pq, k, j); k = j; /* from nowjava - 时 代 Java*/ } } private static void exch(Object[] pq, int i, int j) { Object swap = pq[i-1]; pq[i-1] = pq[j-1]; pq[j-1] = swap; } private static boolean less(Comparable[] pq, int i, int j) { return pq[i-1].compareTo(pq[j-1]) < 0; } public static void sort(Comparable[] pq) { int N = pq.length; for (int k = N/2; k >= 1; k--) sink(pq, k, N); while (N > 1) { exch(pq, 1, N--); sink(pq, 1, N); } } public static void main(String[] args) {