Skip to content

Commit ece145e

Browse files
committed
[U] 更新堆排序实现
1 parent fab524c commit ece145e

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

  • src/main/java/com/bruis/algorithminjava/algorithm/sort

src/main/java/com/bruis/algorithminjava/algorithm/sort/HeapSort.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public static void sort(int[] arr) {
2727
// [.......]k
2828
// [.....] ba
2929
for (int i = n-1; i > 0; i--) {
30+
// 由于上面执行过下沉操作,所以已经是最大堆(但没有排序完)。所以此时swap就将最大值替换到数组末尾。
3031
swap(arr, 0, i);
32+
// 由于siftDown中是判断 2*k+1 < n ,所以就是对n-1进行下沉操作;
3133
siftDown2(arr, i, 0);
3234
}
3335
}
@@ -72,8 +74,7 @@ private static void siftDown2(int[] arr, int n, int k) {
7274
arr[k] = arr[j];
7375
k = j;
7476
}
75-
// 就是这一个替换,让此堆变成了最小堆;
76-
// 这是因为while循环结束后,最小值以及替换到了arr[k]了,所以
77+
// 将最小元素替换到k的位置
7778
arr[k] = e;
7879
}
7980

0 commit comments

Comments
 (0)