Heap Insert Operation Bubble Up in DSA Java - 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.
public void insert(int value) {
heap[size] = value;
int current = size;
size++;
while (current > 0 && heap[current] > heap[(current - 1) / 2]) {
int parent = (current - 1) / 2;
int temp = heap[current];
heap[current] = heap[parent];
heap[parent] = temp;
current = parent;
}
}
This code adds a new value to a max-heap and moves it up until the heap order is correct.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that swaps the new value with its parent if it is larger.
- How many times: At most, this loop runs once per level of the heap, moving up from the inserted node to the root.
Each time we add a new item, it might move up several levels. The heap is a tree with height growing slowly as the heap grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4 swaps |
| 100 | Up to 7 swaps |
| 1000 | Up to 10 swaps |
Pattern observation: The number of swaps 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 grows slowly, only as the height of the heap grows, not with the total number of items directly.
[X] Wrong: "The insert operation always takes constant time because we just add at the end."
[OK] Correct: Sometimes the new item must move up many levels to keep the heap order, so the time depends on the heap height, not just adding at the end.
Understanding how insert works in a heap and its time cost helps you explain efficient priority queues and sorting methods clearly in interviews.
"What if the heap was a min-heap instead of a max-heap? How would the time complexity of bubble up change?"
Practice
Solution
Step 1: Understand the heap property
In a min-heap, every parent node must be smaller than its children.Step 2: Purpose of bubble up
When a new element is added at the bottom, bubble up moves it up to restore the heap property.Final Answer:
To move the new element up until the heap property is restored -> Option CQuick Check:
Bubble up fixes heap by moving new element up [OK]
- Thinking bubble up moves element down
- Confusing bubble up with removing elements
- Assuming bubble up sorts the entire heap
Solution
Step 1: Identify correct swap logic
Swapping requires a temporary variable to hold one value to avoid overwriting.Step 2: Check each option
int temp = arr[parent]; arr[parent] = arr[i]; arr[i] = temp; correctly uses temp to swap arr[parent] and arr[i]. int temp = arr[i]; arr[i] = arr[parent]; arr[parent] = arr[i]; incorrectly sets arr[parent] to the already modified arr[i], causing data loss. Options B and D overwrite without temp, causing errors.Final Answer:
int temp = arr[parent]; arr[parent] = arr[i]; arr[i] = temp; -> Option AQuick Check:
Swap with temp variable = int temp = arr[parent]; arr[parent] = arr[i]; arr[i] = temp; [OK]
- Swapping without a temp variable
- Swapping wrong indices
- Overwriting values causing data loss
[2, 5, 8, 10, 15]. After inserting the value 1 and performing bubble up, what will be the resulting array?Solution
Step 1: Insert 1 at the end
Array becomes [2, 5, 8, 10, 15, 1]. Index of 1 is 5.Step 2: Bubble up 1
Parent index of 5 is (5-1)/2 = 2. Compare arr[2]=8 with 1. Since 1 < 8, swap. Array: [2, 5, 1, 10, 15, 8]. Now index of 1 is 2.Step 3: Continue bubble up
Parent index of 2 is (2-1)/2 = 0. Compare arr[0]=2 with 1. Since 1 < 2, swap. Array: [1, 5, 2, 10, 15, 8].Final Answer:
[1, 2, 5, 10, 15, 8] -> Option BQuick Check:
Bubble up swaps until heap property restored [OK]
- Not swapping enough times
- Swapping with wrong parent index
- Incorrect parent calculation
void bubbleUp(int[] heap, int index) {
while (index > 0) {
int parent = (index - 1) / 2;
if (heap[index] < heap[parent]) {
int temp = heap[index];
heap[index] = heap[parent];
heap[parent] = temp;
index = parent;
}
}
}Solution
Step 1: Analyze loop and index update
The index is updated to parent every iteration, even if no swap occurs.Step 2: Identify bug effect
If no swap happens, index still moves up, causing unnecessary checks and possible infinite loop if heap property is already satisfied.Step 3: Correct fix
Update index = parent only when a swap occurs inside the if block.Final Answer:
The index update should be inside the if block -> Option AQuick Check:
Update index only after swap to avoid wrong moves [OK]
- Updating index outside swap condition
- Wrong parent index formula
- Incorrect swap logic
[20, 15, 18, 10, 12, 17]. You insert the value 19. After performing the bubble up operation, what is the final array?Solution
Step 1: Insert 19 at the end
Array becomes [20, 15, 18, 10, 12, 17, 19]. Index of 19 is 6.Step 2: Bubble up 19 in max-heap
Parent index of 6 is (6-1)/2 = 2. Compare arr[2]=18 with 19. Since 19 > 18, swap. Array: [20, 15, 19, 10, 12, 17, 18]. Now index of 19 is 2.Step 3: Continue bubble up
Parent index of 2 is (2-1)/2 = 0. Compare arr[0]=20 with 19. Since 19 < 20, stop bubbling up.Final Answer:
[20, 15, 19, 10, 12, 17, 18] -> Option DQuick Check:
Bubble up swaps while child > parent in max-heap [OK]
- Stopping bubble up too early
- Swapping with wrong parent index
- Confusing min-heap and max-heap rules
Ready to prove it?
Pass the test and this pattern moves unknown
