Each node, in Floyd's algorithm, in the worst case, sinks to become a leaf. The sink operation involves repeatedly (recursively) swapping the value with one of the children. Let's make the following observation:
Each node at height h makes at most h swaps (down).
So if you are a leaf, at height h=0, you make no swaps down. If you are one level higher, at height h=1, you make at most one swaps down. And so on. Let's make the following observation:
In a complete binary tree with n nodes, there are ⌈2n⌉ nodes at height h=0 (leaves), ⌈4n⌉ nodes at height h=1, ⌈8n⌉ nodes at height h=2, …
In general, there are at most ⌈2h+1n⌉ nodes of height h, so the cost of building a heap is
h=0∑⌊lgn⌋⌈2h+1n⌉O(h)
We can rewrite the above expression as
O⎝⎛h=0∑⌊lgn⌋⌈2h+1n⌉h⎠⎞
Since n does not depend on the value of h, it can be moved out of the fraction
O⎝⎛nh=0∑⌊lgn⌋⌈2h+1h⌉⎠⎞
Further observe that ⌈2h×2h⌉<2hh and we can use the upper-bound within big-O notation.
O⎝⎛nh=0∑⌊lgn⌋2hh⎠⎞
Moreover,
h=0∑⌊lgn⌋2hh<h=0∑∞2hh
And, we can use the upper-bound within big-O notation:
O(nh=0∑∞2hh)
It turns out the summation converges to 2: (It is beyond the scope of this course to prove this, but you can check it out on Wolfram Alpha.)
h=0∑∞2hh=2
Therefore, we get the following which means the heapify operation is linear time.
O(n×2)=O(n)
A more intuitive analysis
Let's consider a perfect binary tree; here is a less accurate but more intuitive analysis:
Height
#nodes
worst-case #swaps
Total work
0
n/2
n/2×0=0
0
1
n/4
n/4×1=n/4
n/4
2
n/8
n/8×2=n/4
n/2
3
n/16
n/16×3<n/8
n/2+n/8
4
n/32
n/32×4=n/8
n/2+n/4
5
n/64
n/64×5<n/16
n/2+n/4+n/16
6
n/128
n/128×6<n/16
n/2+n/4+n/8
⋮
…
…
n/2+n/4+n/8+…
The summation for total work is in O(n).
Resources
For a more detailed analysis of Floyd's algorithm, refer to Page 157 (Chapter 6, Section 3) of CLRS.