コード例 #1
0
def heap_sort(num_arr):
    """
    Returns num_arr sorted in ascending fashion. The original array is not modified.
    Sorting is implemented using heap sort algorithm.
    The algorithm has O(n * log(n)) complexity.
    How it works: The array is put into max heap. The root of max heap is always the largest number. The sorting is done
        by repeated popping of the root.
    This implementation does not conserve the order of equal elements ("unstable sort").
    """
    hp = Heap(num_arr)
    result = []
    while not hp.is_empty():
        result.insert(0, hp.pop_max())
    return result