Heap Insert Operation Bubble Up in DSA Javascript - Time & Space Complexity
When we add a new item to a heap, we need to keep it in order. The bubble up step moves the new item up to the right place.
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.
function bubbleUp(heap) {
let index = heap.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (heap[index] <= heap[parentIndex]) break;
[heap[index], heap[parentIndex]] = [heap[parentIndex], heap[index]];
index = parentIndex;
}
}
This code moves the last element up the heap until it is in the correct spot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that compares and swaps the new element with its parent.
- How many times: At most, it runs once per level of the heap, moving from the bottom to the top.
Each time we bubble up, we move one level higher in the heap tree. The heap height grows slowly as the heap gets bigger.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 to 5 swaps |
| 100 | About 6 to 7 swaps |
| 1000 | About 9 to 10 swaps |
Pattern observation: The number of steps grows slowly, roughly with the height of the heap, which increases as the logarithm of the number of items.
Time Complexity: O(log n)
This means the time to insert and bubble up grows slowly, even if the heap gets very large.
[X] Wrong: "Bubble up takes the same time no matter how big the heap is because it only moves one element."
[OK] Correct: The element may need to move up many levels, and the number of levels grows with the heap size, so time grows too.
Understanding bubble up time helps you explain how heaps keep order efficiently. This skill shows you know how data structures handle growing data smoothly.
"What if we changed the heap to a linked structure instead of an array? How would the time complexity of bubble up 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 a new element is added at the end, bubble up moves it up to restore the max heap property.Final Answer:
To move the new element up until the heap property is restored -> Option AQuick Check:
Bubble up fixes heap by moving element up [OK]
- Thinking bubble up moves element down
- Confusing bubble up with removing elements
- Assuming bubble up sorts entire heap
i and parentIndex in a JavaScript array heap during bubble up?Solution
Step 1: Identify JavaScript swap syntax
JavaScript uses array destructuring to swap: [a, b] = [b, a];Step 2: Apply to heap array
Swapping heap[i] and heap[parentIndex] is done by [heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]];Final Answer:
[heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]]; -> Option AQuick Check:
Destructuring swap syntax = [heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]]; [OK]
- Using Python-style swap syntax
- Calling undefined swap function
- Trying to use non-existent array method
[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
Heap becomes [50, 30, 40, 10, 20, 45]Step 2: Bubble up 45
Parent index of 45 (index 5) is 2 (value 40). Since 45 > 40, swap them. New heap: [50, 30, 45, 10, 20, 40]Final Answer:
[50, 30, 45, 10, 20, 40] -> Option DQuick Check:
Bubble up swaps 45 with 40 [OK]
- Not swapping after insertion
- Swapping with wrong parent index
- Assuming insertion sorts entire heap
while (i > 0 && heap[i] > heap[parentIndex]) {
[heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]];
i = parentIndex;
parentIndex = Math.floor((i - 1) / 2);
} What is the bug if parentIndex is not updated inside the loop?Solution
Step 1: Understand loop variables
Inside the loop, after swap i = parentIndex, but if parentIndex is not updated, i now equals the unchanged parentIndex.Step 2: Consequence of not updating parentIndex
The condition heap[i] > heap[parentIndex] becomes heap[pos] > heap[pos] which is false, causing premature loop termination without full restoration.Final Answer:
Loop terminates prematurely after one swap because parentIndex never changes -> Option CQuick Check:
Without parentIndex update, i == parentIndex stops loop early [OK]
- Forgetting to update parentIndex
- Assuming i update is enough
- Ignoring loop condition effects
[5, 10, 15, 20, 25]. You want to insert 12 and maintain the min heap property using bubble up. Which of the following sequences correctly shows the heap array after each bubble up swap?Solution
Step 1: Insert 12 at the end
Heap becomes [5, 10, 15, 20, 25, 12]Step 2: Bubble up 12
Parent index of 12 (index 5) is 2 (value 15). Since 12 < 15, swap them. New heap: [5, 10, 12, 20, 25, 15]Final Answer:
[5, 10, 15, 20, 25, 12] -> [5, 10, 12, 20, 25, 15] -> Option BQuick Check:
Bubble up swaps 12 with 15 in min heap [OK]
- Swapping with wrong parent
- Not swapping when child is smaller
- Confusing min heap with max heap rules
Ready to prove it?
Pass the test and this pattern moves unknown
