Complexity Analysis

Building the heap

As mentioned in the previous lesson, the time complexity of building a heap is O(N)O(N). This is not obvious right away so let’s analyse the algorithm.

The entire building of the heap can be expressed as NN heapify events. Each heapify event take O(h)O(h) time where hh is the height of the tree.

When I say heapify in this lesson, I mean the top to bottom heapify, the same can be proved for the bottom to up heapify as well.

Each heapify event can be provided with a tighter upper bound than hh ...

Access this course and 1400+ top-rated courses and projects.