Introduction and Insertion Sort
Introduction and Insertion Sort
2.(10 7 ) 2 instructions
= 20,000 sec onds (more than 5.5 hours)
1010 instructions / sec ond
Insertion Sort: This algorithm will help us solve the sorting problem
introduced above. It is an efficient algorithm for sorting a small number of
elements.
INSERTION-SORT(A)
1 for j = 2 to A.length
2 key = A[ j ]
3 // Insert A[ j ] into the sorted sequence A[1.. j − 1]
4 i = j −1
5 while i 0 and A[i] key
6 A[i + 1] = A[i ]
7 i = i −1
8 A[i + 1] = key
Now, let us see how these properties hold for insertion sort
Input Size: In most cases the input size is the number of items in the input.
But in some cases, like multiplying large numbers, the total number of bits
in the input is a better measure.
1 for j = 2 to A.length c1 n
2 key = A[ j ] c2 n −1
3 // Insert A[j] into the sorted sequence
// A[1 .. j – 1] 0 n −1
4 i = j −1 c4 n −1
5 while i 0 and A[i] key c5 t 2 + t 3 + ... + t n
6 A[i + 1] = A[i ] c6 (t 2 − 1) + (t 3 − 1) + ... + (t n − 1)
7 i = i −1 c 7 (t 2 − 1) + (t 3 − 1) + ... + (t n − 1)
8 A[i + 1] = key c8 n −1
The running time depends on the values of t j . These vary according to the
input.
T (n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 (n − 1) + c8 (n − 1)
= (c1 +c 2 +c4 + c5 + c8 )n − (c2 + c4 + c5 + c8 )
T (n) can be expressed as an + b for constants a and b that depend on the given
machine. It is a linear function of n.
Worst-Case: Line 5 is executed j times for each j . This can happen if A
starts out in reverse order. For all j , t j = j,
(n + 2)(n − 1)
t 2 + t 3 + ... + t n = 2 + 3 + ... + n = and
2
n(n − 1)
(t 2 − 1) + (t 3 − 1) + ...(t n − 1) = 1 + 2 + ... + n − 1 = . Hence, T(n) could be
2
expressed as:
2 3 n (n + 2)(n − 1)
t 2 + t 3 + ... + t n = + + ... + = and
2 2 2 4
n
(t2 − 1) + (t3 − 1) + ... + (tn − 1) = (0) + (0.5) + (1) + ... + ( − 1)
2
(n 2 − 3n + 2)
If we simplify the above expression, we get:
4
Hence, we have:
(n + 2)(n − 1) (n 2 − 3n + 2) (n 2 − 3n + 2)
T (n) = c1 n + c 2 (n − 1) + c 4 (n − 1) + c5 + c6 . + c7 . + c8 (n − 1)
4 4 4
For small values of n, Insertion-Sort will probably run very quickly that we
do not care how long it takes.
For large values of n, Insertion-Sort will probably run very slowly, and it
will take a long time and its running time is very important.
T ( n) b c
2
= a + + 2 approaches the constant a and the other two terms go
n n n
toward 0.
When we analyze any algorithm, we are interested in the running time for
large input sizes (large n) and we just consider the dominant term (the term
that grows the fastest). Our analysis of algorithms should not depend on any
particular machine, so we ignore the constant in front of the dominant term.
We only look at the rate of growth as the input size gets larger and larger.
For Insertion-Sort we say the worst-case running time is (n 2 ) , and the best-
case running time is (n) .