LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

Calling removeMin() repeatedly on the min-heap [1, 2, 4, 5, 3] (array from index 1): what does the array look like after each call?

Returns 1 → [2, 3, 4, 5]; returns 2 → [3, 5, 4]; returns 3 → [4, 5]; returns 4 → [5]; returns 5 → []. The keys come out in ascending order.

Array states while draining the heap: 2,3,4,5 then 3,5,4 then 4,5 then 5 then empty

* Each call returns index 1, moves the last key to the front and lets it sink. *

Each call takes index 1, moves the entry at index n to index 1, shrinks the heap by one and downheaps (swap with the smaller child while that child is smaller):

  1. remove 1: 3 moves to the root → [3, 2, 4, 5]. Children of index 1 are 2 and 4; 2 is smaller and 2 < 3: swap → [2, 3, 4, 5]. The only child of index 2 is 5 ≥ 3: stop.
  2. remove 2: 5 moves to the root → [5, 3, 4]. Children 3 and 4; 3 is smaller and 3 < 5: swap → [3, 5, 4]. Index 2 has no children: stop.
  3. remove 3: 4 moves to the root → [4, 5]. Child 5 ≥ 4: stop.
  4. remove 4: [5].
  5. remove 5: [].

The output 1, 2, 3, 4, 5 is the input of the previous insert trace in sorted order. Together, the two traces are heap-sort on a small scale: $n$ inserts followed by $n$ removeMins.

From Quiz: ADS / Priority Queues and Heaps | Updated: Nov 26, 2026