Unit1 Insertion Sort KCS503
Unit1 Insertion Sort KCS503
Before going for growth of functions and asymptotic notation let us see how to analyze an
algorithm.
How to analyze an Algorithm
Let us form a first algorithm, insertion sort, solves the sorting problem((which sort a sequence
of numbers).
The pseudo code for the algorithm is give below.
Pseudo code:
INSERTION-SORT(A)
1 for j ← 2 to length[A]
2 do 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 do A[i + 1] ← A[i ]
7 i ←i − 1
8 A[i + 1] ← key
Example
Analysis of Algorithm
Let us start by presenting the INSERTION-SORT procedure with the time “cost” of each
statement and the number of times each statement is executed. For each j = 2, 3, . . . , n, where
n = length[A], we let t j be the number of times the while loop test in line 5 is executed for that
value of j. When a for or while loop exits in the usual way (i.e., due to the test in the loop
header), the test is executed one time more than the loop body. We assume that comments are
not executable statements, and so they take no time.
The running time of the algorithm is the sum of running times for each statement
executed; a statement that takes ci steps to execute and is executed n times will contribute ci n
to the total running time. To compute T(n), the running time of INSERTION-SORT, we sum the
products of the cost and times columns, obtaining
We must compare each element A[ j ] with each element in the entire sorted subarray A[1 . . j − 1], and
so for j = 2, 3, . . . , n. Noting that
Which is of the form an2+bn+c. A Quadratic function. So in worst case insertion set grows in n2.
The worst-case running time gives a guaranteed upper bound on the running time for
any input.
For some algorithms, the worst case occurs often. For example, when searching, the worst case
often occurs when the item being searched for is not present, and searches for absent items
may be frequent.
Why not analyze the average case? Because it’s often about as bad as the worst case.
Order of growth:
It is described by the highest degree term of the formula for running time. (Drop lower-order terms.
Ignore the constant coefficient in the leading term.)
Example:
We found out that for insertion sort the worst-case running time is of the form an2+bn+c. Drop lower-
order terms. What remains is an2.Ignore constant coefficient. It results in n2.But we cannot say that the
worst-case running time T(n) equals n2 .Rather It grows like n2 . But it doesn’t equal n2.We say that the
running time is Θ (n2) to capture the notion that the order of growth is n2.
We usually consider one algorithm to be more efficient than another if its worst-case running time has a
smaller order of growth.