chapter_sorting/heap_sort/ #517
Replies: 5 comments 10 replies
-
if (l < n && nums[l] > nums[ma])
ma = l;
if (r < n && nums[r] > nums[ma])
ma = r; 这里代码和堆那节siftDowm代码有点不一样,这样不能判断三个里面谁最大吧 |
Beta Was this translation helpful? Give feedback.
3 replies
-
有时候看指针看着头有点晕,心里知道指针指向的是元素的索引,但自己去想总弄混到元素上 |
Beta Was this translation helpful? Give feedback.
1 reply
-
我觉得图片里的那个例子交换元素后进行大根堆调整的过程体现不够明显 |
Beta Was this translation helpful? Give feedback.
4 replies
-
// 建堆操作:堆化除叶节点以外的其他所有节点
for (int i = nums.length / 2 - 1; i >= 0; i--) {
siftDown(nums, nums.length, i);
} 获取最后一个非叶子节点的索引,友情提示 |
Beta Was this translation helpful? Give feedback.
1 reply
-
public static void heapSort(int[] nums){
//初始化堆
for(int i= nums.length/2-1;i>=0;i--){//((nums.length-1)-1)/2
siftDown(nums, nums.length, i);
}
for(int i= nums.length;i>0;i--){//有效长度(迭代有效长度)
swap(nums,0,i-1);//首和有效区间的尾交换,即0和有效长度-1
siftDown(nums,i-1,0);//向下堆化,此时有效长度已经-1了,因为已经“出去一个”
}
} 直接从nums.length-1开始逆序遍历有点不好理解,虽然结果对了 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
chapter_sorting/heap_sort/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_sorting/heap_sort/
Beta Was this translation helpful? Give feedback.
All reactions