Heap Insert Operation Bubble Up in DSA C - Time & Space Complexity
When we add a new item to a heap, we need to keep it in order. This process is called bubble up.
We want to know how the time to do this grows as the heap gets bigger.
Analyze the time complexity of the following code snippet.
void bubbleUp(int heap[], int index) {
while (index > 0) {
int parent = (index - 1) / 2;
if (heap[index] <= heap[parent])
break;
int temp = heap[index];
heap[index] = heap[parent];
heap[parent] = temp;
index = parent;
}
}
This code moves the newly added element up the heap until the heap property is restored.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that swaps the new element with its parent.
- How many times: At most, once per level of the heap, moving from the inserted node up to the root.
Each bubble up step moves the element one level higher in the heap tree.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 swaps (levels) |
| 100 | About 7 swaps |
| 1000 | About 10 swaps |
Pattern observation: The number of steps grows slowly, roughly with the height of the heap, which increases as the logarithm of n.
Time Complexity: O(log n)
This means the time to insert grows slowly as the heap gets bigger, only increasing with the height of the tree.
[X] Wrong: "Bubble up takes linear time because it might move through all elements."
[OK] Correct: The bubble up only moves up the tree levels, not all elements, so it grows with the tree height, not total elements.
Understanding bubble up time helps you explain how heaps keep order efficiently, a key skill for many coding problems.
"What if the heap was a ternary heap (each node has 3 children) instead of binary? How would the time complexity change?"
Practice
Solution
Step 1: Understand heap property
A max-heap requires each parent to be greater than or equal to its children.Step 2: Purpose of bubble up
When inserting, bubble up moves the new element up if it is larger than its parent to restore heap property.Final Answer:
To restore the heap property by moving the new element up if it is larger than its parent -> Option AQuick Check:
Bubble up fixes heap by moving new element up [OK]
- Thinking bubble up sorts the entire heap
- Confusing bubble up with removing elements
- Assuming bubble up swaps root with last element
i in a max-heap array heap?Solution
Step 1: Identify correct bubble up condition
Bubble up swaps while current element is greater than its parent at index (i-1)/2.Step 2: Check code correctness
while (i > 0 && heap[i] > heap[(i - 1) / 2]) { int temp = heap[i]; heap[i] = heap[(i - 1) / 2]; heap[(i - 1) / 2] = temp; i = (i - 1) / 2; } correctly uses a while loop with condition and swaps elements, updating index i to parent.Final Answer:
while (i > 0 && heap[i] > heap[(i - 1) / 2]) { int temp = heap[i]; heap[i] = heap[(i - 1) / 2]; heap[(i - 1) / 2] = temp; i = (i - 1) / 2; } -> Option DQuick Check:
Bubble up uses parent index (i-1)/2 and swaps if child > parent [OK]
- Using < instead of > in condition
- Using for loop instead of while
- Swapping with next element instead of parent
heap = [50, 30, 40, 10, 20], what will be the heap array after inserting 45 and performing bubble up?Solution
Step 1: Insert 45 at the end
Insert 45 at index 5: heap = [50, 30, 40, 10, 20, 45]Step 2: Bubble up 45
Parent of index 5 is (5-1)/2=2 with value 40. Since 45 > 40, swap them. New heap: [50, 30, 45, 10, 20, 40]Final Answer:
[50, 30, 45, 10, 20, 40] -> Option BQuick Check:
Inserted 45 bubbled up swapping with 40 [OK]
- Not swapping after insertion
- Swapping with wrong parent index
- Placing 45 at wrong position
while (i > 0 && heap[i] < heap[(i - 1) / 2]) {
int temp = heap[i];
heap[i] = heap[(i - 1) / 2];
heap[(i - 1) / 2] = temp;
i = (i - 1) / 2;
}
What is the error in this code?Solution
Step 1: Analyze comparison operator
Bubble up in max-heap swaps when child is greater than parent, so condition should be heap[i] > heap[parent].Step 2: Identify error
Code uses heap[i] < heap[parent], which is wrong and breaks max-heap property.Final Answer:
The comparison operator should be > instead of < to maintain max-heap property -> Option CQuick Check:
Max-heap bubble up swaps if child > parent [OK]
- Using < instead of > in condition
- Incorrect parent index formula
- Skipping swaps in bubble up
[60, 40, 55, 20, 30, 50]. You insert 65 at the end and perform bubble up. What is the final heap array after bubble up completes?Solution
Step 1: Insert 65 at the end
Heap becomes [60, 40, 55, 20, 30, 50, 65]. Index of 65 is 6.Step 2: Bubble up 65
Parent index = (6-1)/2 = 2, value 55. Since 65 > 55, swap: [60, 40, 65, 20, 30, 50, 55].Step 3: Continue bubble up
New index = 2. Parent index = (2-1)/2 = 0, value 60. Since 65 > 60, swap: [65, 40, 60, 20, 30, 50, 55].Final Answer:
[65, 60, 55, 20, 30, 50, 40] -> Option AQuick Check:
Inserted 65 bubbled up swapping with 55 then 60 [OK]
- Stopping bubble up after one swap
- Swapping with wrong parent index
- Not updating index after swap
Ready to prove it?
Pass the test and this pattern moves unknown
