Skip to content

Commit 5de5705

Browse files
committed
heap sort
1 parent fc97bcd commit 5de5705

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/main/java/grey/algorithm/code11_heap/Code_0003_HeapSort.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212
// 测评:https://www.lintcode.com/problem/464
1313
//测评链接:https://www.luogu.com.cn/problem/P1177
1414
public class Code_0003_HeapSort {
15-
1615
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);
16+
for (int i = 0; i < arr.length; i++) {
17+
heapInsert(arr,i );
2118
}
22-
// 注意:这里要保存一个变量,因为n在循环里面会变化
23-
int size = n;
19+
// 大根堆
20+
// 此时,最大元素已经在0号位置
21+
int size = arr.length;
2422
while (size > 0) {
25-
heapify(arr, 0, size);
26-
swap(arr, 0, --size);
23+
swap(arr, 0,--size);
24+
heapify(arr, 0 , size);
2725
}
2826
}
2927

0 commit comments

Comments
 (0)