def sift(nums, low, high):
# 构造大根堆(假设左右子树已是大根堆)。
# 索引由0开始。
# 递归版本,空间复杂度不为O(1)
# left, right = 2*low+1, 2*low+2
# large_index = low
# if left<=high and nums[low]= 0:
sift(nums, i, high)
i -= 1
for i in range(high, 0, -1):
nums[0], nums[i] = nums[i], nums[0]
sift(nums, 0, i-1)
return nums
inputs = [1, 2, 34, 35, 23, 56, 2, 13]
print(HeapSort(inputs)) # [1, 2, 2, 13, 23, 34, 35, 56]