Python 排序算法之堆排序,使用 heapq 实现

"""
堆排序 - 完全二叉树 - 最大堆,最小堆
借助内置的 heapq 模块
"""


def heapsort_use_heap(iterable):
    from heapq import heappush, heappop
    items = []
    for value in iterable:
        heappush(items, value)
    return [heappop(items) for i in range(len(items))]


def test_heapsort():
    import random
    arr = list(range(10))

    random.shuffle(arr)
    print(arr)
    res = heapsort_use_heap(arr)
    print(res)


test_heapsort()
原文地址:https://www.cnblogs.com/jiaoran/p/14584275.html