Next:
Up:
Previous:
Example: Insertion Sort
n
= length(
A
)
t
j
= number of times the while loop
test
is executed for that value of
j
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
5
A[i+1] = A[i]
c
5
6
i = i - 1
c
6
7
A[i+1] = key
c
7
n
- 1
Next:
Up:
Previous: