Skip to content

Commit 8c005c5

Browse files
authored
Update Heap.java
1 parent 7250c4e commit 8c005c5

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

十大排序算法_Java/Heap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public int[] heapSort(int[] nums) {
2222
// 堆调整的逻辑:父节点与子节点较大者交换,最终最大值会升到根节点
2323
private void max_heapify(int[] nums, int root) {
2424
int maxIndex = root;
25-
int left = root * 2, right = root * 2 + 1;
25+
// 索引从0开始
26+
int left = root * 2 + 1, right = root * 2 + 2;
2627
// 如果有左孩子,且左孩子大于父节点,则将最大指针指向左孩子
2728
if (left < n && nums[left] > nums[maxIndex])
2829
maxIndex = left;

0 commit comments

Comments
 (0)