AlgoFrames

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