2
If this notation is not absolutely clear, read the pseudocode conventions outlined on pages 1920 of
the text.
In particular, notice that the left arrow "
←
" stands for the assignment operator, and that loop
bodies and conditional branches are indicated solely by indentation, not by braces or other punctuation
characters.
Also there are no semicolons, parenthesis, or brackets, and local variables need not be
declared before use.
Notice that on the
j
th
iteration of loop 27, the subarray
)]
1
(
1
[

j
A
L
is already sorted, while
]
[
n
j
A
L
is unsorted.
Steps 37 have the effect of inserting
j
A
into its correct position in
)]
1
(
1
[

j
A
L
, thus
expanding the sorted section by exactly 1.
n
j
j
j
A
A
A
A
A
1
1
1
L
L
L
L
L
L
L
L
+

Exercise:
Trace InsertionSort(
A
) on
)
6
,
2
,
7
,
4
,
1
,
3
,
5
,
8
(
=
A
We wish to determine the
run time
)
(
n
T
of this algorithm as a function of the input size
n
.
This
analysis should be, as far as possible, independent of the computing machine that is executing the
algorithm.
Let
k
c
denote the cost of step
k
.
We remain uncommitted as to just what units or even
what quantity
k
c
measures.
This could be processor time in seconds, or power consumed in watts, or
even some appropriate monetary unit.
As we shall see, details of this kind are not critical to the
analysis.
Thus for instance,
2
c
and
3
c
represent the costs of assignment operations, while
1
c
and
4
c
are the costs of performing tests of loop repetition conditions.
Notice that