# 第二种
nums = [2, 3, 5, 1, 54, 23, 132]
heapq.heapify(nums)
print([heapq.heappop(heap) for _ in range(len(nums))]) # 堆排序结果
# out: [1, 2, 3, 5, 23, 54, 132]
上面代码中第二种
print([heapq.heappop(heap) for _ in range(len(nums))]) # 堆排序结果 的heap应该是nums
Python标准库模块之heapq该模块提供了堆排序算法的实现。堆是二叉树,最大堆中父节点大于或等于两个子节点,最小堆父节点小于或等于两个子节点。 创建堆 heapq有两种方式创建堆, 一种是使用一个空列表,...