|
|
|
|
|
|
|
|
|
|
|
Introduction to Algorithms : Recurrence |
|
|
|
|
|
|
|
|
|
|
|
|
|
- Hence, the recurrence relation for Insertion sort is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
T(n) |
= T(n-1) + cn, if n > 1 |
|
|
|
|
|
|
|
|
|
= 1 if n = 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- There are three main approach to solve recurrence relations. They are substitution, iterative and master theorem methods. In this notes we discuss iterative approach only. For other methods see Algorithms by
Cormen et al.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
T(n) |
= T(n-1) + cn |
|
= T(n-2) + c(n-1) + cn |
|
= T(n-3) + c(n-2) + c(n-1) + cn |
|
.
. |
|
= T(1) + 2c + 3c + ............. + c(n-2) + c(n-1) + cn |
|
= 1 + 2c + 3c + ............. + c(n-2) + c(n-1) + cn |
|
< c( 1 + 2 + 3 + ............. + n-2 + n-1 + n) |
|
= c (n (n+1)/2) |
|
= c + c n/2 |
|
= O( ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Prev |
|
|
|
|
|
|
|
|
|
|
|
|