python 排序模块 ———— heapq(学习笔记)

  

from heapq import *
def heasort(initi):# 排序
h=[]
for value in initi:
heappush(h,value)#将每一个item进入heap中
return [heappop(h) for i in range(len(h))]
def merge_sort(seq):#堆排序
if len(seq) <= 1:#如果seq的list长度小于1 直接返回
return seq
else:
middle = int(len(seq)/2)
left = merge_sort(seq[:middle])
right = merge_sort(seq[middle:])
return list(merge(left, right))
if __name__ == '__main__':
seq=[1,3,6,2,4]
print(merge_sort(seq))
print(heasort([11,2,31,4]))

python 排序模块 ———— heapq(学习笔记)

上一篇:BZOJ ac100题存档


下一篇:专业运维配的vsftpd.conf