堆排序python

def swap_param(L, i, j): #元素交换
    L[i], L[j] = L[j], L[i]
    return L


def heap_adjust(L, start, end):  #调整堆
    temp = L[start]

    i = start
    j = 2 * i

    while j <= end:
        if (j < end) and (L[j] < L[j + 1]):
            j += 1
        if temp < L[j]:
            L[i] = L[j]
            i = j
            j = 2 * i
        else:
            break
    L[i] = temp


def heap_sort(L):
    L_length = len(L) - 1

    first_sort_count = L_length // 2
    for i in range(first_sort_count):  #把序列调整为一个大根堆
        heap_adjust(L, first_sort_count - i, L_length)

    for i in range(L_length - 1):  #把堆顶元素和堆末尾的元素交换
        L = swap_param(L, 1, L_length - i)
        heap_adjust(L, 1, L_length - i - 1)

    return [L[i] for i in range(1, len(L))]


def main():
    L = [50, 16, 30, 10, 60,  90,  2, 80, 70]
    L.insert(0,0)
    print( heap_sort(L))


if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/dl_007/article/details/83046081