The Wayback Machine - https://web.archive.org/web/20240118164520/https://www.geeksforgeeks.org/time-complexity-of-building-a-heap/
Open In App
Related Articles

Time Complexity of building a heap

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Consider the following algorithm for building a Heap of an input array A. 

BUILD-HEAP(A) 

    heapsize := size(A); 

    for i := floor(heapsize/2) downto 1 

        do HEAPIFY(A, i); 

    end for 

END

A quick look over the above algorithm suggests that the running time is O(n * lg(n))   since each call to Heapify costs O(lg(n))  and Build-Heap makes O(n)  such calls. 

This upper bound, though correct, is not asymptotically tight. 

We can derive a tighter bound by observing that the running time of Heapify depends on the height of the tree ‘h’ (which is equal to lg(n), where n is a number of nodes) and the heights of most sub-trees are small. The height ’h’ increases as we move upwards along the tree. Line-3 of Build-Heap runs a loop from the index of the last internal node (heapsize/2) with height=1, to the index of root(1) with height = lg(n). Hence, Heapify takes a different time for each node, which is:

For finding the Time Complexity of building a heap, we must know the number of nodes having height h. For this we use the fact that, A heap of size n has at most \left \lceil \frac{n}{2^{h+1}} \right \rceil  nodes with height h. 

a  to derive the time complexity, we express the total cost of Build-Heap as-

 T(n) = \sum_{h = 0}^{lg(n)}\left \lceil \frac{n}{2^{h+1}} \right \rceil * O(h)= O(n * \sum_{h = 0}^{lg(n)}\frac{h}{2^{h}})= O(n * \sum_{h = 0}^{\infty}\frac{h}{2^{h}})

Step 2 uses the properties of the Big-Oh notation to ignore the ceiling function and the constant 2(2^{h+1} = 2.2^h  ). Similarly in Step three, the upper limit of the summation can be increased to infinity since we are using Big-Oh notation. Sum of infinite G.P. (x < 1)

 \sum_{n = 0}^{\infty}{x}^{n} = \frac{1}{1-x}

On differentiating both sides and multiplying by x, we get

 \sum_{n = 0}^{\infty}n{x}^{n} = \frac{x}{(1-x)^{2}}

Putting the result obtained in (3) back in our derivation (1), we get

 = O(n * \frac{\frac{1}{2}}{(1 - \frac{1}{2})^2})= O(n * 2)= O(n)

Hence Proved that the Time complexity for Building a Binary Heap is O(n). 

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 17 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials