Ejemplo n.º 1
0
def heapsort(array):
    """Implements heapsort in-place using heap.Heap.  Insert all
    the values in (via heapify in O(n) ) and the read them
    out in max order, inserting into the correct place in the
    orig list.

    This uses an additional O(n) space since we create another copy in the
    Heap.  We could do this in place if the heap tracked the max heap size
    separate from the max array size
    """

    heapy = Heap(array)
    for i in reversed(range(len(array))):
        array[i] = heapy.extract_max()