Heap Extract Min or Max Bubble Down in DSA C - Time & Space Complexity
We analyze how long it takes to remove the top element from a heap and restore its order.
How does the time needed grow as the heap gets bigger?
Analyze the time complexity of the following code snippet.
int extractMin(int heap[], int *size) {
int min = heap[0];
heap[0] = heap[*size - 1];
(*size)--;
int i = 0;
while (2 * i + 1 < *size) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int smallest = left;
if (right < *size && heap[right] < heap[left]) {
smallest = right;
}
if (heap[i] <= heap[smallest]) break;
int temp = heap[i];
heap[i] = heap[smallest];
heap[smallest] = temp;
i = smallest;
}
return min;
}
This code removes the smallest element from a min-heap and restores heap order by moving the new root down.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves the root element down the heap to restore order.
- How many times: It repeats until the element reaches a leaf or no swap is needed, at most once per heap level.
Each step moves down one level in the heap tree, which grows slowly as the heap size increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 to 4 swaps |
| 100 | About 6 to 7 swaps |
| 1000 | About 9 to 10 swaps |
Pattern observation: The number of steps grows slowly, roughly proportional to the height of the heap, which is much smaller than the total number of elements.
Time Complexity: O(log n)
This means the time to remove the top element grows slowly, increasing only with the height of the heap, not the total number of elements.
[X] Wrong: "Extracting the min takes linear time because we might check every element."
[OK] Correct: The heap structure lets us restore order by moving down only one path, so we never check all elements.
Understanding this helps you explain efficient priority queue operations, a common topic in coding interviews.
"What if the heap was a max-heap instead of a min-heap? How would the time complexity of extract change?"
Practice
bubble down operation in a min-heap after extracting the minimum element?Solution
Step 1: Understand extract-min operation
When the minimum element (root) is removed, the last element is moved to the root position.Step 2: Apply bubble down to restore heap
The bubble down operation moves this element down the tree to restore the min-heap property by swapping with smaller children.Final Answer:
To restore the heap property by moving the root element down to its correct position -> Option BQuick Check:
Bubble down fixes heap after extract-min [OK]
- Thinking bubble down inserts new elements
- Assuming bubble down only swaps root with last element
- Confusing bubble down with bubble up
heap[i] and heap[j] in C during bubble down?Solution
Step 1: Recognize C swap syntax
In C, swapping requires a temporary variable to hold one value during exchange.Step 2: Verify temp = heap[i]; heap[i] = heap[j]; heap[j] = temp; uses temp variable correctly
temp = heap[i]; heap[i] = heap[j]; heap[j] = temp; uses a temporary variable to swap values correctly, while others use invalid or non-C syntax.Final Answer:
temp = heap[i]; heap[i] = heap[j]; heap[j] = temp; -> Option AQuick Check:
C swap needs temp variable [OK]
- Using Python or pseudocode swap syntax in C
- Trying to swap without temp variable
- Using invalid operators like <->
[2, 4, 5, 10, 7, 6], after extracting the minimum and performing bubble down, what is the resulting heap array?Solution
Step 1: Remove root and replace with last element
Remove 2, move last element 6 to root: [6, 4, 5, 10, 7]Step 2: Bubble down 6 to restore min-heap
Compare 6 with children 4 and 5; swap with smallest child 4: [4, 6, 5, 10, 7]. Next, 6's children are 10 and 7, both larger, so stop.Final Answer:
[4, 6, 5, 10, 7] -> Option DQuick Check:
Bubble down swaps with smaller child [OK]
- Not swapping with the smallest child
- Stopping bubble down too early
- Replacing root incorrectly
void bubbleDown(int heap[], int size, int i) {
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < size && heap[left] > heap[largest])
largest = left;
if (right < size && heap[right] > heap[largest])
largest = right;
if (largest != i) {
int temp = heap[i];
heap[i] = heap[largest];
heap[largest] = temp;
bubbleDown(heap, size, largest);
}
}Solution
Step 1: Check child index calculations
Left = 2*i+1 and right = 2*i+2 are correct for zero-based heap arrays.Step 2: Verify bubble down logic
Code finds largest among node and children, swaps if needed, then recurses on swapped child. This matches max-heap bubble down logic.Final Answer:
No bug, code correctly bubbles down in max-heap -> Option AQuick Check:
Correct child indices and recursion [OK]
- Confusing left/right child index formulas
- Wrong recursive call index
- Swapping condition misunderstanding
[20, 18, 15, 10, 12, 9, 8]. After extracting the maximum element and performing bubble down, what is the final heap array?Solution
Step 1: Remove root and replace with last element
Remove 20, move last element 8 to root: [8, 18, 15, 10, 12, 9]Step 2: Bubble down 8 to restore max-heap
Compare 8 with children 18 and 15; swap with largest child 18: [18, 8, 15, 10, 12, 9]. Next, 8's children are 10 and 12; swap with largest child 12: [18, 12, 15, 10, 8, 9]. Now 8's children are out of range, stop.Final Answer:
[18, 12, 15, 10, 8, 9] -> Option CQuick Check:
Bubble down swaps with largest child until heap property restored [OK]
- Swapping with smaller child instead of largest
- Stopping bubble down too early
- Incorrectly replacing root element
Ready to prove it?
Pass the test and this pattern moves unknown
