6 questions. Covers Heap sort in Python.
Questions 1 to 5 of 6
i
Use the arrows to put the steps in order.
2 * i + 1
2 * i + 2
end
end - 1
len(a) // 2 - 1
python def sift_down(a, i, size): while True: left = 2 * i + 1 right = 2 * i + 2 largest = i if left < size and a[left] > a[largest]: largest = left if right < size and a[right] > a[largest]: largest = right if largest == i: return a[i], a[largest] = a[largest], a[i] i = largest a = [3, 8, 1, 6, 5] for i in range(len(a) // 2 - 1, -1, -1): sift_down(a, i, len(a)) print(a)
python def sift_down(a, i, size): while True: left = 2 * i + 1 right = 2 * i + 2 largest = i if left < size and a[left] > a[largest]: largest = left if right < size and a[right] > a[largest]: largest = right if largest == i: return a[i], a[largest] = a[largest], a[i] i = largest b = [4, 6, 1, 8, 2] for i in range(len(b) // 2 - 1, -1, -1): sift_down(b, i, len(b)) b[0], b[-1] = b[-1], b[0] sift_down(b, 0, len(b) - 1) print(b)
n
3 question(s) left on this page.