堆排序(内置模块 heapq )(NB)

博客地址:https://www.cnblogs.com/zylyehuo/

# _*_coding:utf-8_*_

import heapq  # q->queue 优先队列
import random

li = list(range(10))
random.shuffle(li)

print(li)

heapq.heapify(li)  # 建堆(小根堆)

n = len(li)
for i in range(n):
    print(heapq.heappop(li), end='')
    if i < n - 1:
        print(',', end='')

posted @ 2023-08-12 22:07  zylyehuo  阅读(9)  评论(0编辑  收藏  举报