import heapq
# 第一种
"""
函数定义:
heapq.heappush(heap, item)
- Push the value item onto the heap, maintaining the heap invariant.
heapq.heappop(heap)
- Pop and return the smallest item from the heap, maintaining the heap invariant.
If the heap is empty, IndexError is raised. To access the smallest item without popping it, use heap[0].
"""
nums = [2, 3,
【Python】heapq模块
最新推荐文章于 2025-04-09 14:23:52 发布
本文介绍了Python的heapq模块,通过heappush和heappop函数实现堆排序。示例中展示了两种方法:直接使用heappush和heappop进行元素入堆和出堆,以及先使用heapify将列表转换为堆再进行排序。堆排序结果一致,均为[1, 2, 3, 5, 23, 54, 132]。

最低0.47元/天 解锁文章
5148

被折叠的 条评论
为什么被折叠?



