Lecture 13(Decrease and Conquer)
Lecture 13(Decrease and Conquer)
Lecture #13
Decrease-and-Conquer
a problem of size n
(instance)
subproblem
of size n-1
solution to the
subproblem
• Insertion Sort
• Breadth First Search(BFS)
• Depth First Search(DFS)
• Topological Sorting
• Connected Components
Decrease(by constant factor)-and-Conquer
a problem of size n
(instance)
subproblem
of size n/2
solution to the
subproblem
12
11
Insertion Sort
6 10 24 36
12
12
Insertion Sort
6 10 24 3
6
12
13
Insertion Sort
input array
5 2 4 6 1 3
sorted unsorted
14
Insertion Sort
15
Insertion Sort
INSERTION-SORT(A)
Alg.: INSERTION-SORT(A[0..n-1])
for j ← 2 to n
for i do
← key
1 to←n-1A[ j ]
do
v← A[A[ij ]] into the sorted sequence A[1
Insert key. . j -1]
i← j - A[
Insert 1 i] into the sorted sequence A[0 . . i -1]
j ←i i>- 01 and A[i] > key
while
do A[ij +
while >= 1] 0← and
A[i] A[j] > v
i←i–1
do A[j + 1] ← A[j]
A[i + 1] ← key
j←j–1
• Insertion sort – sorts the elements in place
A[j + 1] ← v
• Insertion sort – sorts the elements in place
16
Insertion Sort
Analysis:
•In the worst case ,A[j]>v is executed the
largest number of times i.e. , for every j=i-1,
………..,0.
•Since v=A[i] ,it happens if and only