m = len(x) n = len(x)//2 left = 2*n + 1 right = 2*n + 2 if left < m and x[left] <= x[n]: smallest = left else: smallest = n if right < m and x[right] <= x[smallest]: smallest = right if not largest == n: x[n], x[smallest] = x[smallest], x[n] heapify(x)

by the way the last call is just a recursive call of the function