面试常见算法题---堆排序python

堆排序注意判断最后一个根节点只有左节点,没有右节点的情况。

class Solution():
    def heap_sort(self, heap):
        root = (len(heap)-2)//2
        for i in range(root, -1, -1):
            left = 2 * i + 1
            if heap[left] > heap[i]:
                heap[left], heap[i] = heap[i],heap[left]
            right = left + 1
            if i == (len(heap)-2)//2 and (len(heap)) % 2 == 0:
                right = left
            if heap[right] > heap[i]:
                heap[right], heap[i] = heap[i], heap[right]
        return heap
    def main(self,heap):
        for i in range(len(heap)-2):
            heap[i:] = self.heap_sort(heap[i:])
            print(heap)

你可能感兴趣的:(面试常见算法题---堆排序python)