Bottom Up Or Not At All
def sift_down(a, i, n):
while True:
smallest, l, r = i, 2 * i + 1, 2 * i + 2
if l < n and a[l] < a[smallest]: smallest = l
if r < n and a[r] < a[smallest]: smallest = r
if smallest == i: return
a[i], a[smallest] = a[smallest], a[i]
i = smallest
def heapify(a):
n = len(a)
for i in range(n // 2 - 1, -1, -1):
sift_down(a, i, n)
What is a after heapify(a) when a = [5, 3, 8, 1, 9, 2, 7]?
Sign in to answer questions and track your progress
Sign In