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 involves moving the new item up the heap until it fits.
We want to know how the time to do this grows as the heap gets bigger.
Analyze the time complexity of the bubble up step during heap insertion.
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.
Look for loops or repeated steps that take time.
- Primary operation: Comparing and swapping the new element with its parent.
- How many times: At most once per level of the heap, moving up from the inserted node to the root.
The heap is a tree with levels growing as the number of elements 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 swaps grows slowly, roughly with the height of the heap, which increases logarithmically as the heap size grows.
Time Complexity: O(log n)
This means the time to insert and bubble up grows slowly, only as the height of the heap increases with more elements.
[X] Wrong: "Bubble up takes the same time no matter how big the heap is because it just swaps once."
[OK] Correct: Actually, the new element might need to move up many levels, and the number of levels grows as the heap grows, so time depends on heap size.
Understanding how bubble up works and its time cost helps you explain heap insertions clearly and confidently in interviews.
"What if the heap was a ternary heap (each node has 3 children) instead of binary? How would the time complexity of bubble up change?"
Practice
Solution
Step 1: Understand heap property
In a min-heap, every parent node is smaller than its children. When inserting, this property might break.Step 2: Purpose of bubble up
The bubble up operation moves the newly inserted element up the tree if it is smaller than its parent, restoring the heap property.Final Answer:
To restore the heap property by moving the new element up if it is smaller than its parent -> Option AQuick Check:
Bubble up fixes heap order by moving smaller elements up [OK]
- Thinking bubble up moves element down
- Confusing bubble up with removing elements
- Assuming bubble up sorts the entire heap
Solution
Step 1: Understand swapping logic
To swap two elements, store one in a temporary variable, assign the second to the first, then assign temp to the second.Step 2: Check each option
int temp = heap[parent]; heap[parent] = heap[i]; heap[i] = temp; correctly stores heap[parent] in temp, then swaps heap[parent] and heap[i]. int temp = heap[i]; heap[i] = heap[parent]; heap[parent] = heap[i]; swaps incorrectly by storing heap[i] first.Final Answer:
int temp = heap[parent]; heap[parent] = heap[i]; heap[i] = temp; -> Option AQuick Check:
Swap uses temp variable correctly [OK]
- Overwriting one element before saving it
- Swapping without temp variable causing data loss
- Confusing indices in swap
[2, 5, 8, 10, 15], what will be the heap array after inserting 1 and performing bubble up?Solution
Step 1: Insert 1 at the end
Insert 1 at index 5: [2, 5, 8, 10, 15, 1]Step 2: Bubble up 1
Parent of index 5 is index 2 (value 8). Since 1 < 8, swap: [2, 5, 1, 10, 15, 8]. Now index 2's parent is index 0 (value 2). Since 1 < 2, swap again: [1, 5, 2, 10, 15, 8].Final Answer:
[1, 2, 5, 10, 15, 8] -> Option CQuick Check:
Bubble up swaps until heap property restored [OK]
- Not swapping enough times to restore heap
- Swapping with wrong parent index
- Inserting element 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 bug in this code?Solution
Step 1: Check parent index formula
Parent index in zero-based heap is (i - 1) / 2, which is correct.Step 2: Check loop condition and swap
Loop continues while current element is smaller than parent, swapping them correctly. Indices used in swap are correct.Final Answer:
No bug; the code correctly performs bubble up -> Option BQuick Check:
Parent index and swap logic are correct [OK]
- Using wrong parent index formula
- Reversing comparison operator in condition
- Swapping with wrong indices
[20, 15, 18, 8, 10, 5]. You insert 17 and perform bubble up. What is the final heap array?Solution
Step 1: Insert 17 at the end
Insert 17 at index 6: [20, 15, 18, 8, 10, 5, 17]Step 2: Bubble up 17 in max-heap
Parent of index 6 is index 2 (value 18). Since 17 < 18, no swap needed. Heap remains [20, 15, 18, 8, 10, 5, 17].Step 3: Verify parent index
Parent of 6 is (6-1)/2 = 2 (18). 17 < 18, so no swap for max-heap bubble up.Step 4: Re-examine options
Options A and B show incorrect bubbling up (A swaps with wrong parent 15, B swaps with parent 18). Since 17 < 18, no swap. Heap stays [20, 15, 18, 8, 10, 5, 17] matching [20, 15, 18, 8, 10, 5, 17].Final Answer:
[20, 15, 18, 8, 10, 5, 17] -> Option DQuick Check:
Bubble up swaps only if child > parent in max-heap [OK]
- Swapping when child is smaller than parent
- Confusing max-heap with min-heap rules
- Incorrect parent index calculation
Ready to prove it?
Pass the test and this pattern moves unknown
