python 堆排序

2021-06-20 11:04

阅读:627

标签:EAP   排序   port   list   sort   imp   htm   bre   html   

import copy

 

def heap_sort(hlist):

    def heap_adjust(parent):

        child = 2 * parent + 1  # left child

        while child  heap[child]:

                    child += 1  # right child

            if heap[parent] >= heap[child]:

                break

            heap[parent], heap[child] = heap[child], heap[parent]

            parent, child = child, 2 * child + 1

 

    heap, hlist = copy.copy(hlist), []

    for i in range(len(heap) // 2, -1, -1):

        heap_adjust(i)

    while len(heap) != 0:

        heap[0], heap[-1] = heap[-1], heap[0]

        hlist.insert(0, heap.pop())

        heap_adjust(0)

    return hlist

 

hlist = heap_sort([4,5,6,7,3,2,6,9,8])

print(hlist)

  

python 堆排序

标签:EAP   排序   port   list   sort   imp   htm   bre   html   

原文地址:https://www.cnblogs.com/sea-stream/p/9689056.html


评论


亲,登录后才可以留言!