Algorithms and Data Structures · Senior · Question 41 of 75

What is the complexity of building a binary heap from n unsorted elements with sift-down (heapify)?

Explanation

Most nodes are near the bottom and sift down only a little: half the nodes move 0 levels, a quarter 1 level and so on. The sum converges to O(n). Inserting elements one by one would be O(n log n).

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 41

Test yourself

Take the Algorithms and Data Structures · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz