当前位置:   article > 正文

最小堆建立和堆排序

最小堆

堆树的定义:

(1)堆树是一颗完全二叉树;

(2)堆树中某个节点的值总是不大于或不小于其孩子节点的值;

(3)堆树中每个节点的子树都是堆树。

当父节点的键值总是大于或等于任何一个子节点的键值时为最大堆。 当父节点的键值总是小于或等于任何一个子节点的键值时为最小堆。如下图所示,左边为最小堆,右边为最大堆。

 

数组与堆

无序数组转化成原始的二叉堆:

453261

 

构建最小堆:

元素下沉思想:从无序数组的最后往前遍历,堆树从下层到上层依次搭建完成

1 从叶子节点(无序数组的最后一个元素)出发,把当前的parent设置为无序数组中的最后一个数(也就是叶子节点),依次往前遍历,当parent是叶子节点,就不做操作

2 当parent遍历到非叶子节点,此时,它就有左子节点和右子节点或者只有左子节点。需要做的操作:先在左右子节点中找到最小元素所在的索引,然后将左右子节点中的最小元素与parent的节点,进行比较,如果小于,则交换,同时更新parent和child的位置。否则不交换,退出当前循环。

3 “元素下沉”的终止条件就是父节点的元素值不大于其任意左右子节点的元素值,或者当前父节点无子节点(即当前节点为叶子节点)。

  1. def heap_min(heap):
  2. for parent in range(len(heap)-1,-1,-1):
  3. child = 2*parent+1
  4. while child <len(heap):
  5. if child+1<len(heap) and heap[child+1]<heap[child]:
  6. child += 1
  7. if heap[parent]<= heap[child]:
  8. break
  9. heap[parent],heap[child] = heap[child],heap[parent]
  10. parent,child = child,2*child+1
  11. return heap
  12. print(heap_min([4,5,3,2,6,1]))

输出结果是:构建了最小堆

堆排序:

是指利用堆(最大堆、最小堆)这种数据结构所设计的一种排序算法。堆是一个完全二叉树的结构,并同时满足如下性质:即子结点的键值或索引总是小于(或者大于)它的父节点。

  1. def sift_down(arr, start, end):
  2. root = start
  3. while True:
  4. # 从root开始对最大堆调整
  5. child = 2 * root + 1
  6. if child > end:
  7. break
  8. # 找出两个child中交大的一个
  9. if child + 1 <= end and arr[child] < arr[child + 1]:
  10. child += 1
  11. if arr[root] < arr[child]:
  12. # 最大堆小于较大的child, 交换顺序
  13. arr[root], arr[child] = arr[child], arr[root]
  14. # 正在调整的节点设置为root
  15. root = child
  16. else:
  17. # 无需调整的时候, 退出
  18. break
  19. def heap_sort(arr):
  20. # 从最后一个有子节点的孩子还是调整最大堆
  21. first = len(arr) // 2 - 1
  22. for start in range(first, -1, -1):
  23. sift_down(arr, start, len(arr) - 1)
  24. # 将最大的放到堆的最后一个, 堆-1, 继续调整排序
  25. for end in range(len(arr) -1, 0, -1):
  26. arr[0], arr[end] = arr[end], arr[0]
  27. sift_down(arr, 0, end - 1)
  28. def main():
  29. # [7, 95, 73, 65, 60, 77, 28, 62, 43]
  30. # [3, 1, 4, 9, 6, 7, 5, 8, 2, 10]
  31. l = [3, 1, 4, 9, 6, 7, 5, 8, 2, 10]
  32. print (l)
  33. heap_sort(l)
  34. print (l)
  35. if __name__ == "__main__":
  36. main()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/957687
推荐阅读
相关标签
  

闽ICP备14008679号