next up previous
Next: Analysis Up: l1 Previous: Example: Insertion Sort

Analysis


\begin{displaymath}T(n) = c_1 n + c_2(n-1) + c_3(n-1) + c_4 \sum_{j=2}^n t_j +
...
...sum_{j=2}^n (t_j - 1) + c_6 \sum_{j=2}^n (t_j - 1) +
c_7(n-1) \end{displaymath}

Best Case:
Array already sorted, $t_j$ = 1 for all $j$

\begin{eqnarray*}
T(n) & = & c_1 n + c_2(n-1) + c_3(n-1) + c_4(n-1) + c_5(0) + c...
...+ c_7) \\
& = & an + b \;\;\; ({\rm linear} \; {\rm in} \; n)
\end{eqnarray*}



Worst Case:
Array in reverse order, $t_j$ = $j$ for all $j$
Note that \(\sum_{j=1}^n j = \frac{n(n+1)}{2}\)

\begin{eqnarray*}
T(n) & = & c_1 n + c_2(n-1) + c_3(n-1) + c_4(\frac{n(n+1)}{2} ...
...
& = & an^2 + bn + c \;\;\; ({\rm quadratic} \; {\rm in} \; n)
\end{eqnarray*}



Average Case:
Check half of array on average, $t_j$ = j/2 for all $j$
$T(n) = an^2 + bn + c$


next up previous
Next: Analysis Up: l1 Previous: Example: Insertion Sort