AlgoFrames

Extract the minimum

Standard · Trace the algorithm · about 8 min

Remove the minimum from the min-heap [2, 4, 3, 7, 5, 8, 6, 9]. The last value, 9, has already moved to the root.

Predict the first swap, then arrange the array once 9 has sunk to its place.

Example 1

Input:

{"values":[2,4,3,7,5,8,6,9]}

Output:

[3,4,6,7,5,8,9]

The returned minimum is 2.

Constraints

  • The trace uses the heap shown

Learn the idea first: Binary heaps: array layout, sift up, sift down, a visual lesson.

More Binary heaps: array layout, sift up, sift down problems