# To heapify subtree rooted at index i.
def maxHeapify(list, heapSize, i):
largest = i
leftChild = 2 * i
rightChild = 2 * i + 1
while leftChild < heapSize and list[largest] < list[leftChild]:
largest = leftChild
while rightChild < heapSize and list[largest] < list[rightChild]:
largest = rightChild
if largest != i:
list[i], list[largest] = list[largest], list[i]
maxHeapify(list, heapSize, largest)
def heapSort(list):
heapSize = len(list)
for i in range(heapSize//2, 0, -1):
maxHeapify(list, heapSize, i)
for i in range(heapSize, 0, -1):
list[i], list[1] = list[1], list[i]
maxHeapify(list, i, 1)
list = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
heapSort(list)
print("Sorted array is: ")
for i in range(len(list)):
print(list[i], end=" ")
Since the index array starts from 1 in heap sort, I have solved this problem in this way. But I am getting an error "IndexError: list index out of range." It would be nice if anyone would help me to find out the bug of this code. I have attached a picture below of the output I got.

for i in range(heapSize, 0, -1):starts withi = heapSize, butheapSizeis not a valid index into the list, because alistin python is indexed starting at 0, ending atheapSize-1.listas variable name, it's already taken.