1
19
Analyzing Insertion Sort as a
Recursive Algorithm
Recursive Algorithm
l
Basic idea:
divide and conquer
»
Divide into 2 (or more) subproblems.
»
Solve each subproblem recursively.
»
Combine the results.
l
Insertion sort is just a bad divide & conquer !
»
Subproblems: (a) last element
(b) all the rest
»
Combine: find where to put the last element
Lecture 2, April 5, 2001
20
Recursion for Insertion Sort
Recursion for Insertion Sort
l
We get a recursion for the running time T(n):
l
Formal proof: by induction.
l
Another way of looking:
split into
n
subproblems,
merge
one by one.
1
2
(
1)
for
1
( )
1
for
1
( )
(
1)
(
2)
(
1)
(
3)
(
2)
(
1)
...
(
)
n
i
T n
n
n
T n
n
T n
T n
n
T n
n
n
T n
n
n
n
i
n
=

+
=
=
=

+
=

+

+
=

+

+

+
=
= ∑
=Θ
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document