title: Heap sort, index of array starting at index 1
body: <pre><code># To heapify subtree rooted at index i.
  def maxHeapify(list, heapSize, i):
      largest = i
      leftChild = 2 * i 
      rightChild = 2 * i + 1
      while leftChild &lt; heapSize and list[largest] &lt; list[leftChild]:
          largest = leftChild
      while rightChild &lt; heapSize and list[largest] &lt; 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(&quot;Sorted array is: &quot;)
  for i in range(len(list)):
      print(list[i], end=&quot; &quot;)
  </code></pre>
  <p>Since the index array starts from 1 in heap sort, I have solved this problem in this way. But I am getting an error &quot;IndexError: list index out of range.&quot; 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.</p>
  <p><img src="https://i.stack.imgur.com/s6B3p.png" alt="output" /></p>