next up previous
Next: Heapsort: Recursive Construction Up: Heap Sort Previous: Heap Sort

Heapsort Construction

Construct Heap:
for $i= \lfloor n/2 \rfloor$ downto $1$
Restore-heap($i$)












Time Complexity of Iterative Algorithm for Heap Construction:

\begin{displaymath}\sum_{h=1}^{\lfloor\log n\rfloor}\lceil n/2^{h+1} \rceil O(h)\end{displaymath}



\begin{displaymath}=O\left (n\sum_{h=1}^{\log n} (h/2^{h} )\right)\end{displaymath}


$=O(n)$ (pp. 135)


Consider $\sum_{h=1}^{\log n}h/2^{h}$
Let

$\displaystyle x$ $\textstyle =$ $\displaystyle 1/2+2/2^{2}+3/2^{3}+4/2^{4}+ \cdots+y/2^{y}$ (1)
$\displaystyle 2x$ $\textstyle =1+$ $\displaystyle 2/2^{1}+3/2^{2}+4/2^{3}+\cdots+y/2^{y-1}$ (2)
$\displaystyle x$ $\textstyle =1+$ $\displaystyle 1/2+1/2^{2}+\cdots+1/2^{y-1}$ (3)
    $\displaystyle -y/2^{y}$ (4)
  $\textstyle =$ $\displaystyle \frac{(1/2)^{y}-1}{1/2-1}-y/2^{y}$ (5)
  $\textstyle =$ $\displaystyle 2(1-(1/2)^y)-y/2^{y}$ (6)
  $\textstyle \leq 2$   (7)



Sushil_Prasad 2014-09-25