Heaps & Priority Queues
Heap Operations & HeapSort
Master Heap operations: bubble-up, bubble-down, O(N) heapify construction, and in-place HeapSort.
Last Updated: August 2, 2026
•
15 min read
1. Introduction
What are Heap Operations & HeapSort?
insert (adding elements), extract (removing the root), and heapify (converting an arbitrary array into a valid heap).Why study them?
Knowing how to implement these algorithms is crucial. It tests your ability to manipulate array structures representing virtual binary trees, balance shape vs value invariants, and optimize sorting.Where is it Used?
2. Mental Models
Bubble-Up (Sift-Up): Rising Helium Balloon
Imagine a helium balloon released at the bottom of the tree:Bubble-Down (Sift-Down): Sinking Stone
Imagine a heavy stone dropped at the root:3. Core Algorithms & Implementations
Insertion (Bubble-Up)
1. Append the new element at the end of the array (maintaining the complete tree shape). 2. Compare the element with its parent. If it violates the heap property, swap them. 3. Repeat step 2 recursively moving up. TakesO(log N) time.
Extraction (Bubble-Down)
1. Replace the root element with the last element in the array. 2. Remove the last element (shrinking the active array size). 3. Compare the new root with its children. Swap with the smaller child (for Min-Heap) or larger child (for Max-Heap) if the property is violated. 4. Repeat step 3 recursively moving down. TakesO(log N) time.
Heapify (O(N) Construction)
Instead of inserting N elements one-by-one (which takes O(N log N)), we can construct a heap in O(N) time:
(N/2 - 1) and work backwards to index 0, running siftDown at each step.O(N): Nodes near the bottom of the tree have very small heights and only sift down a few levels. The majority of nodes reside at these lower levels, bounding the sum of heights to O(N) operations.4. Visual Trace: Sift-Down Extraction
Extracting root 40 from Max-Heap [40, 15, 30, 10]:
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test structural knowledge:O(N) time instead of O(N log N)?" -> Explain that sift-down height complexity is proportional to node height. The mathematical sum of heights in a complete tree bounds operations to linear O(N) time.O(1) auxiliary space), whereas MergeSort takes O(N) space. However, HeapSort is unstable and has poor cache locality.Common Mistakes
Warning: 1. Implementing Heapify with Sift-Up: Building a heap by calling
siftUp from index 0 to N takes O(N log N) time. You must use siftDown working backwards from N/2 - 1 to achieve O(N) time.> 2. Wrong Swap Targets: During sift-down, swapping with the wrong child. In a Max-Heap, you must swap with the larger child. Swapping with the smaller child violates the heap property.
7. Summary
O(N) construction sifting down from N/2 - 1 backwards.O(N log N) sorting utilizing max-heaps.8. Quiz
Question 1: Why does HeapSort use a Max-Heap to sort an array in ascending order?
Answer: Because we swap the root (which is the maximum element) with the last element of the active array. This places the largest elements at the end of the array first, sorting it in ascending order in-place.Question 2: What is the last non-leaf node index in an array of size 10?
Answer:4. Calculated as 10 / 2 - 1 = 5 - 1 = 4.
Question 3: What is the worst-case space complexity of HeapSort?
Answer:O(1) auxiliary space, since sorting is performed entirely in-place by swapping elements inside the input array.
Question 4: True or False: HeapSort is a stable sorting algorithm.
Answer: False. Heap operations involve long-distance swaps across child and parent indices, which can change the relative order of duplicate elements.Question 5: What is the maximum number of swaps performed during a heap insertion in a heap of size N?
Answer:\lfloor log_2 N \rfloor swaps, since the element can bubble up at most from leaf level to root.