Predict the output
What does this print?
import heapq
xs = [9, 4, 7, 1, 8, 2]
heapq.heapify(xs)
print(xs[0], xs[1] < xs[3] or xs[1] < xs[4], sorted(xs) == xs)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 1 True False
Right: the minimum is on top, parents are no larger than children, but the list is not sorted.
Heap order is weaker than sorted order, which is exactly why building it costs only O(n).
Why not the others?
- B 1 True True. heapify does not sort the list.
- C 9 False False. heapify moves the smallest value to index 0.