Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ece145e

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

File tree

1 file changed

+3
-2
lines changed
  • src/main/java/com/bruis/algorithminjava/algorithm/sort

1 file changed

+3
-2
lines changed

‎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)

AltStyle によって変換されたページ (->オリジナル) /