by simonask · 1y ago · 0 comments
That way, roughly half the nodes are leaves (requiring no work), a quarter are at the second-to-last level (requiring at most 1 comparison/swap), an eighth at the third-to-last level (requiring at most 2 comparisons/swaps), and so on. Summing up 1 n/4 + 2 n/8 + ... gets you O(n) total complexity.
See https://en.wikipedia.org/wiki/Heapsort?useskin=monobook#Vari...