publicstaticvoidsort(Comparable[] a){ int n = a.length; // 堆构造 // 核心思路:从数组中间位置,从右向左逐个将元素 sink 来构造堆 // 也就是从最后一个子堆开始,逐个将子堆有序化 // 当到最后一个子堆时,也就是整个堆,下沉第一个元素,确保整个堆有序化 for (int k = n/2; k >= 1; k--) sink(a, k, n); // 堆排序 while (n > 1) { // 核心思路:将最大值交换到数组最后一列 // 将交换到第一个的元素,在剩下的 n-1 个元素中做下沉 // 使得 n-1 个元素的堆恢复到有序状态,即始终确保堆第一个元素为最大值 // 如此反复,直到只剩下第一个元素 // 整个数组变成有序状态 exch(a, 1, n--); sink(a, 1, n); } }
privatestaticvoidsink(Comparable[] a, int k, int n){ while (2*k <= n) { // 找出较大的子节点 int j = 2*k; if (j < n && less(a, j, j+1)) j++; // 如果比子节点大,跳出 if (!less(a, k, j)) break; // 交换,并循环下沉到合适位置 exch(a, k, j); k = j; } }
// 数组从 a[1] 开始为有效元素 privatestaticbooleanless(Comparable[] a, int i, int j){ return a[i].compareTo(a[j]) < 0; }
privatestaticvoidexch(Object[] a, int i, int j){ Object swap = a[i]; a[i] = a[j]; a[j] = swap; }
// 存储元素数组 transient Object[] queue; // non-private to simplify nested class access // 元素个数 privateint size = 0; privatefinal Comparator<? super E> comparator; // 构造方法 publicPriorityQueue(){...} publicPriorityQueue(int initialCapacity){...} publicPriorityQueue(Comparator<? super E> comparator){...} publicPriorityQueue(int initialCapacity, Comparator<? super E> comparator){...} publicPriorityQueue(Collection<? extends E> c){...} publicPriorityQueue(PriorityQueue<? extends E> c){...} publicPriorityQueue(SortedSet<? extends E> c){...} // 优先队列中增加一个元素 publicbooleanadd(E e){return offer(e);} // 数组最后一列加入新元素,并将新元素上浮到合适位置 publicbooleanoffer(E e){ if (e == null) thrownew NullPointerException(); modCount++; int i = size; if (i >= queue.length) grow(i + 1); size = i + 1; if (i == 0) queue[0] = e; else siftUp(i, e); returntrue; } // 取出最小值 public E peek(){return (size == 0) ? null : (E) queue[0];} publicbooleanremove(Object o){...} publicbooleancontains(Object o){...} publicintsize(){...} publicvoidclear(){...} // 取出最小值后,将最后一列元素交换并下沉到合适位置 public E poll(){ if (size == 0) returnnull; int s = --size; modCount++; E result = (E) queue[0]; E x = (E) queue[s]; queue[s] = null; if (s != 0) siftDown(0, x); return result; } privatevoidsiftUp(int k, E x){ if (comparator != null) siftUpUsingComparator(k, x); else siftUpComparable(k, x); }
@SuppressWarnings("unchecked") // 上浮 privatevoidsiftUpComparable(int k, E x){ Comparable<? super E> key = (Comparable<? super E>) x; while (k > 0) { int parent = (k - 1) >>> 1; Object e = queue[parent]; if (key.compareTo((E) e) >= 0) break; queue[k] = e; k = parent; } queue[k] = key; }
@SuppressWarnings("unchecked") privatevoidsiftUpUsingComparator(int k, E x){...}
privatevoidsiftDown(int k, E x){ if (comparator != null) siftDownUsingComparator(k, x); else siftDownComparable(k, x); }
@SuppressWarnings("unchecked") // 下沉 privatevoidsiftDownComparable(int k, E x){ Comparable<? super E> key = (Comparable<? super E>)x; int half = size >>> 1; // loop while a non-leaf while (k < half) { int child = (k << 1) + 1; // assume left child is least Object c = queue[child]; int right = child + 1; if (right < size && ((Comparable<? super E>) c).compareTo((E) queue[right]) > 0) c = queue[child = right]; if (key.compareTo((E) c) <= 0) break; queue[k] = c; k = child; } queue[k] = key; }
@SuppressWarnings("unchecked") privatevoidsiftDownUsingComparator(int k, E x){...} }