堆排序的python实现
import math
def build_heap(arr):
# 创建堆
for i in range(1, len(arr)):
s = i
p = math.ceil(s / 2) - 1
while p >= 0:
if arr[s] > arr[p]:
arr[s], arr[p] = arr[p], arr[s]
else:
break
s = p
p = math.ceil(s / 2) - 1
return arr
def sort(arr):
# 创建排序列表
result_list = []
while arr:
arr = build_heap(arr)
result_list.append(arr.pop(0))
return result_list
l = [5, 2, 7, 8, 6, 1, 4, 9, 10, 1, 2, 3, 4, 100]
print(sort(l))
网站题目:堆排序的python实现
链接URL:http://scyanting.com/article/ieeshe.html