堆排序算法
我需要 HeapSort 算法对数组的元素进行排序,使得数组的所有元素即 [19 18 14 15 5 7 13 3 8] 都按非递减顺序排列。
I need the algorithm of HeapSort for sorting the elements of the array, such that all the elements of the array i.e [19 18 14 15 5 7 13 3 8] are in non-decreasing order.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此处了解堆排序。还提供了一个很好的伪代码。
Read about Heapsort here. A nice pseudocode has also been provided.
堆排序非常简单。您抓取所有元素,将它们放入 堆 (在您的情况下,最大-heap)以任何顺序,然后从堆中抓取它们(使用delete-max操作),然后它们就全部排序了。
Heapsort is pretty simple. You grab all elements, put them in a heap (in your case, a max-heap) in any order and then grab them back from the heap (with the delete-max operation) and they come all sorted up.
实际上,您可以使用 IF-free(branchless)堆排序
Actually, you can use IF-free (branchless) heap sort