堆排序

堆排序是在二叉堆的基础上实现的,可先看
二叉堆

堆排序算法步骤

1.把无序数组构建成最大堆
2.循环交换集合尾部元素到堆顶,并调节堆产生新的堆顶


def heap_sort(array=[]):
    '''
    堆排序:时间复杂度O(nlogn)
    空间复杂度O(1)
    堆排序的话主要是堆的构建把
    '''
    # 1.把无序数组构建成最大堆
    for i in range((len(array)-2)//2, -1, -1):
        down_adjust(i, len(array), array)
    # 2.循环交换集合尾部元素到堆顶,并调节堆产生新的堆顶
    for i in range(len(array)-1, 0, -1): # 逆序
        # 最后一个元素和第一元素进行交换
        temp = array[i] #最后一个元素赋值给temp
        array[i] = array[0] #第一个元素赋值给逆序的最后一个元素
        array[0] = temp # 把最后一个元素赋值给第一个元素
        # 下沉调整最大堆
        down_adjust(0, i, array) #执行下沉操作


def down_adjust(parent_index, length, array=[]):
    # temp保存父节点值,用于最后的赋值
    temp = array[parent_index]
    child_index = 2 * parent_index + 1
    while child_index < length:
        # 如果有右孩子,且右孩子大于左孩子的值,则定位到右孩子
        if child_index+1 < length and array[child_index+1] > array[child_index]:
            child_index += 1
        # 如果父节点大于等于任何一个孩子的值,直接跳出
        if temp >= array[child_index]:
            break
        # 无需真正交换,单向赋值即可
        array[parent_index] = array[child_index]
        parent_index = child_index
        child_index = 2 * child_index + 1
    array[parent_index] = temp


my_array = list([3, 4, 14, 1, 5, 6, 7, 8, 1, -1, 0, 9, 11])
heap_sort(my_array)
print(my_array)



posted @ 2021-12-25 15:17  索匣  阅读(29)  评论(0编辑  收藏  举报