Spot the bug: sift down
Standard · Find the bug · about 8 min
siftDown(heap, i) should move the value at index i down until the min-heap order holds below it. It returns the heap. It works for some heaps and fails for others.
Click the faulty line, then fix it.
Example 1
Input:
[[9,3,2],0]
Output:
[2,3,9]
The buggy code returns [3, 9, 2].
Constraints
- 0 ≤ heap.length ≤ 10,000
- Everything below index i is already a heap
Learn the idea first: Binary heaps: array layout, sift up, sift down, a visual lesson.
More Binary heaps: array layout, sift up, sift down problems
- Parent and child indices Warm-up
- Which array is a min-heap? Warm-up
- Insert into a heap Warm-up
- Is this array a min-heap? Warm-up
- Extract the minimum Standard
- Implement push and pop Standard
- Implement a max-heap with decrease-key Challenge