Quiz Entry - updated: 2026.11.26
Inserting the keys 4, 5, 3, 2, 1 in this order into an empty min-heap: what does the array (index 1 onwards) look like after each insert?
[4] → [4, 5] → [3, 5, 4] → [2, 3, 4, 5] → [1, 2, 4, 5, 3].
* Each new key is appended at the end and swapped towards the front while its parent is larger. *
Each insert appends at index n + 1 and then upheaps (swap with the parent at index ⌊i/2⌋ while the parent is larger):
- insert 4:
[4]. A single node is a heap. - insert 5:
[4, 5]. Parent 4 ≤ 5, no swap. - insert 3: placed at index 3 →
[4, 5, 3]. Parent (index 1) is 4 > 3: swap →[3, 5, 4]. - insert 2: placed at index 4 →
[3, 5, 4, 2]. Parent (index 2) is 5 > 2: swap →[3, 2, 4, 5]. Parent (index 1) is 3 > 2: swap →[2, 3, 4, 5]. - insert 1: placed at index 5 →
[2, 3, 4, 5, 1]. Parent (index 2) is 3 > 1: swap →[2, 1, 4, 5, 3]. Parent (index 1) is 2 > 1: swap →[1, 2, 4, 5, 3].
As a tree, the final heap is 1 → (2, 4), 2 → (5, 3).
Notice that the result is not sorted (5 comes before 3), and that a different insertion order of the same keys would generally give a different heap. Both are expected: the heap only guarantees the order along each root-to-leaf path.