Next:
Analysis
Up:
CSE 2320: Algorithms and
Previous:
Operations
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
Copyright © 1998 The University of Texas at Arlington