next up previous
Next: Analysis Up: l1 Previous: Operations

Example: Insertion Sort

Insertion-Sort(A) Cost Times (Iterations)
1 $\;\;\;\;\;$for j = 2 to length(A) $c_1$ $n$
2 $\;\;\;\;\;$$\;\;\;\;\;$key = A[j] $c_2$ $n - 1$
3 $\;\;\;\;\;$$\;\;\;\;\;$i = j - 1 $c_3$ $n - 1$
4 $\;\;\;\;\;$$\;\;\;\;\;$while i $>$ 0 and A[i] $>$ key $c_4$ $\sum_{j=2}^n t_j$
5 $\;\;\;\;\;$$\;\;\;\;\;$$\;\;\;\;\;$A[i+1] = A[i] $c_5$ $\sum_{j=2}^n t_j - 1$
6 $\;\;\;\;\;$$\;\;\;\;\;$$\;\;\;\;\;$i = i - 1 $c_6$ $\sum_{j=2}^n t_j - 1$
7 $\;\;\;\;\;$$\;\;\;\;\;$A[i+1] = key $c_7$ $n - 1$


next up previous
Next: Analysis Up: l1 Previous: Operations