Skip to content

Commit d562840

Browse files
committed
adds more comments to heapsort
1 parent 21ffa6c commit d562840

File tree

1 file changed

+8
-3
lines changed
  • src/main/java/edu/princeton/cs/algs4

1 file changed

+8
-3
lines changed

src/main/java/edu/princeton/cs/algs4/Heap.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ private Heap() { }
5353
*/
5454
public static void sort(Comparable[] pq) {
5555
int n = pq.length;
56+
57+
// heapify phase
5658
for (int k = n/2; k >= 1; k--)
5759
sink(pq, k, n);
58-
while (n > 1) {
59-
exch(pq, 1, n--);
60-
sink(pq, 1, n);
60+
61+
// sortdown phase
62+
int k = n;
63+
while (k > 1) {
64+
exch(pq, 1, k--);
65+
sink(pq, 1, k);
6166
}
6267
}
6368

0 commit comments

Comments
 (0)