next up previous
Next: Analysis Up: CSE 2320: Algorithms and 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, tj = 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, tj = 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, tj = j/2 for all j
T(n) = an2 + bn + c



Copyright © 1998 The University of Texas at Arlington