next up previous
Next: Up: Previous:

Example: Insertion Sort

Insertion-Sort(A) Cost Times (Iterations)
1 $\;\;\;\;\;$for j = 2 to length(A) c1 n
2 $\;\;\;\;\;$ $\;\;\;\;\;$key = A[j] c2 n - 1
3 $\;\;\;\;\;$ $\;\;\;\;\;$i = j - 1 c3 n - 1
4 $\;\;\;\;\;$ $\;\;\;\;\;$while i > 0 and A[i] > key c4 $\sum_{j=2}^n t_j$
5 $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$A[i+1] = A[i] c5 $\sum_{j=2}^n t_j - 1$
6 $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$i = i - 1 c6 $\sum_{j=2}^n t_j - 1$
7 $\;\;\;\;\;$ $\;\;\;\;\;$A[i+1] = key c7 n - 1


next up previous
Next: Up: Previous: