We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fc97bcd commit 5de5705Copy full SHA for 5de5705
src/main/java/grey/algorithm/code11_heap/Code_0003_HeapSort.java
@@ -12,18 +12,16 @@
12
// 测评:https://www.lintcode.com/problem/464
13
//测评链接:https://www.luogu.com.cn/problem/P1177
14
public class Code_0003_HeapSort {
15
-
16
public static void heapSort1(int[] arr) {
17
- int n = arr.length;
18
- // O(N*logN)
19
- for (int i = 0; i < n; i++) {
20
- heapInsert(arr, i);
+ for (int i = 0; i < arr.length; i++) {
+ heapInsert(arr,i );
21
}
22
- // 注意:这里要保存一个变量,因为n在循环里面会变化
23
- int size = n;
+ // 大根堆
+ // 此时,最大元素已经在0号位置
+ int size = arr.length;
24
while (size > 0) {
25
- heapify(arr, 0, size);
26
- swap(arr, 0, --size);
+ swap(arr, 0,--size);
+ heapify(arr, 0 , size);
27
28
29
0 commit comments