def big_endian(lists,start,end):
root = start
child = root*2+1
while child<=end:
if child+1<=end and lists[child]<lists[child+1]:
child = child+1
if lists[root]<lists[child]:
lists[root],lists[child]=lists[child],lists[root]
root = child
child=root*2+1
else:
break
print('big_endian函数',lists)
def Heep(lists):
first = len(lists)//2-1
for start in range(first,-1,-1):
big_endian(lists,start,len(lists)-1)
print('第一个for循环,构建最大堆结束')
for end in range(len(lists)-1,0,-1):
lists[0],lists[end]=lists[end],lists[0]
big_endian(lists,0,end-1)
return lists
if __name__ == '__main__':
list_demo = [3,5,10,8,16,2,4,7]
print(Heep(list_demo))
最小堆排序
最新推荐文章于 2023-12-28 21:03:24 发布