1 Analysis of Algorithm
1 Analysis of Algorithm
Decide
Decideonon:algorithm
algorithm
design
design
techniques
techniquesetc.
Design an algorithm
Correctness
Prove correctness
Efficiency
Analyze efficiency
n
5. A[i+1] A[i] c5 t
j =2
j −1
6. i i-1 c6 n
t
j =2
j −1
7. A[i+1] key c7
n -1
(tj is the number of times the while loop in line 4 is executed for that value of j)
• The running time, T(n), of the insertion algorithm is the sum of
running times for each statement executed, i.e.:
=c1n+ c2(n-1)+ c3(n-1)+ c4nj=2 tj+ c5nj=2 (tj-1)+ c6nj=2(tj-1)+ c7(n-1)
Best Case Analysis of Insertion Sort
• Occurs if the array contains an already sorted values
–For each j = 2, 3, 4,…, n, we then find that A[i] ≤ key in line 4
when i has its initial value of j – 1.
–Thus tj=1 for j = 2, 3,…, n, and line 5 and 6 will be executed 0
times
• The best case running time is
T(n) = c1n + c2(n-1) + c3(n-1) + c4(n-1) + c7(n-1)
= (c1 + c2 + c3 + c4 + c7)n – (c2 + c3 + c4 + c7)
–This running time can be expressed as an + b for constants a and
b that depends on the statement cost ci;
• it is thus a linear function of n
Worst Case Analysis of Insertion Sort
• Occurs if the array contains values that are in reverse sorted
order, that is in decreasing order
• We must compare each element A[j] with each element in
the entire sorted subarray A[1..j-1]. So, tj = j for j = 2,3,…,n.
n n
n(n + 1) n n
n(n − 1)
t =
j =2
j
j =2
j=
2
− 1 (t j − 1) = ( j − 1) =
j =2 j =2 2
Therefore the worst case running time of INSERTION-SORT is T(n)
n(n + 1) n(n − 1) n(n − 1)
= c1n + c2 (n − 1) + c3 (n − 1) + c4 ( − 1) + c5 ( ) + c6 ( ) + c7 (n − 1)
2 2 2
c4 + c5 + c6 2 c4 c5 c6
=( )n + (c1 + c2 + c3 + + + + c7 )n + (c2 + c3 + c4 + c7 )
2 2 2 2
–This worst case running time can be expressed as an2 + bn + c for
constants a, b, c, it is thus a quadratic function on n
Average Case Analysis of Insertion Sort
• Suppose that we randomly choose n numbers and apply
insertion sort
• How long does it take to determine where in subarray
A[1..j-1] to insert the element A[j]?
–On average, half the elements in A[1..j-1] are less than A[j], and
half the elements are greater
–On average, therefore, we check half the subarray A[1..j-1], so tj =
j/2 and T(n) will still be in the order of n2,
• This average case running time can then be expressed as
quadratic function, an2 + bn + c for constants a, b, c,
which is the same as worst case
• In summary, the running time of insertion sort for
–Best case: an – b
–Worst case: an2 + bn - c
–Average case: an2 + bn - c
Asymptotic Analysis
• When analyzing the running time or space usage of programs, we usually
try to estimate the time or space as a function of the input size.
– For example, when analyzing the worst case running time of an
insertion algorithm, we say the running time of insertion sort is, T(n) =
an2 + bn - c, for some constants a, b & c.
• The asymptotic behavior of a function f(n) (such as f(n)=c*n or f(n)=c*n 2,
etc.) refers to the growth of f(n) as n gets large. We typically ignore small
values of n, since we are usually interested in estimating how slow the
program will be on large inputs.
– A good rule of thumb is: the slower the asymptotic growth rate, the
better the algorithm.
• By this measure, a linear algorithm; f(n)=d*n+k, is always asymptotically
better than a quadratic one; f(n)=c*n2+q. That is because for any given
(positive) c, k, d & q there is always some n at which the magnitude of
c*n2+q overtakes d*n+k.
– For moderate values of n, the quadratic algorithm could very well take
less time than the linear one. However, the linear algorithm will
always be better for sufficiently large inputs.
Asymptotic analysis
• There are five notations used to describe a running time function:
Big-Oh, Big-Omega, Theta, Little-o, little-omega
• Demonstrating that a function T(n) is in big-O (or others) of a
function f(n) requires that we find specific constants C and no for
which the inequality holds
• The following points are facts that can be used for efficiency
comparison
• f(n) = 2n + 6