You are on page 1of 30

Decrease-and-Conquer

Decrease-and-Conquer

1. Reduce problem instance to smaller


instance of the same problem
2. Solve smaller instance
3. Extend solution of smaller instance
to obtain solution to original
instance

.1

.2
.3
3 Types of Decrease and Conquer

Decrease by a constant (usually by 1):


insertion sort

Decrease by a constant factor (usually


by half)
binary search

insertion sort

binary search
What s the difference?
Consider the problem of exponentiation:
Compute an
Brute Force: multiply 1 by a n times (bottom-up)
Decrease by one: (top-down)

Decrease by constant factor:

an
n a
Insertion Sort
Idea: like sorting a hand of
playing cards
Start with an empty left hand
and the cards facing down on
the table.
Remove one card at a time
from the table, and insert it
into the correct position in the
left hand
The cards held in the left hand
are sorted
Insertion Sort

Insertion Sort
Insertion Sort

Insertion Sort
The Insertion sort Algorithm

The Insertion
sort algorithm
also views the
array as having
a sorted side and
an unsorted side.
The Insertion sort Algorithm

The sorted
side starts
with just the
first element,
which is not
necessarily
the smallest
element.
The Insertion sort Algorithm

The sorted
side grows by
taking the
front element
from the
unsorted
side...
The Insertion sort Algorithm

...and
inserting it in
the place that
keeps the
sorted side
arranged
from small to
large.
The Insertion sort Algorithm
In this
example, the
new element
goes in front of
the element
that was
already in the
sorted side.
The Insertion sort Algorithm

Sometimes
we are lucky
and the new
inserted item
doesn't need
to move at
all.
The Insertion sort Algorithm

Sometimes
we are
lucky twice
in a row.
How to Insert One Element

Copy the
new element
to a separate
location.
How to Insert One Element
Shift elements
in the sorted
side, creating
an open space
for the new
element.
How to Insert One Element

Continue
shifting
elements...
How to Insert One Element

Continue
shifting
elements...
How to Insert One Element

Continue
shifting
elements...
How to Insert One Element

...until you
reach the
location for
the new
element.
How to Insert One Element
Copy the
new element
back into
the array, at
the correct
location.
How to Insert One Element
The last
element
must also
be inserted.
Start by
copying it...
A Quiz
A Quiz

Four items
are shifted.
A Quiz
Four items
are shifted.
And then the
element is
copied back
into the array.
Insertion Sort
Insertion sort keeps making the left side of
the array sorted until the whole array is
sorted.
Basic Algorithm:
Repeat the following steps until value in
proper position
Step 1. Pull out current value into a
temp variable
Step 2. Check index s value - if less
than temp then move it left.

index
Insertion Sort
INSERTION-SORT(A)
for to n
do
Insert into the sorted sequence

while and
do

Insertion sort sorts the elements in place


Analysis of Insertion Sort
INSERTION-SORT(A) cost times
for j 2 to n
do key A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i j-1
n
while i > 0 and A[i] > key j 2 j
t
n
do A[i + 1] A[i] j 2
(t j 1)
n
i i 1 j 2
(t j 1)

A[i + 1] key

n n n
T ( n) c1n c2 (n 1) c4 (n 1) c5 tj c6 tj 1 c7 tj 1 c8 (n 1)
j 2 j 2 j 2
Best Case Analysis
The array is already sorted
upon the first time the while loop test is
run (when i = j -1)

t =1

(Linear in n)
n n n
T (n) c1n c2 (n 1) c4 (n 1) c5 tj c6 tj 1 c7 tj 1 c8 (n 1)
j 2 j 2 j 2

(when i = j -1) while


t =1

(n )

n n n
T (n) c1n c2 (n 1) c4 (n 1) c5 tj c6 tj 1 c7 tj 1 c8 (n 1)
j 2 j 2 j 2
Worst Case Analysis
The array is in reverse sorted order
Always in while loop test
Have to compare with all elements to the left of the
-th position compare with elements t
n n n
n(n 1) n (n 1) n (n 1)
j j 1 ( j 1)
j 1 2 j 2 2 j 2 2

n(n 1) n(n 1) n(n 1)


T (n) c1n c2 (n 1) c4 (n 1) c5 1 c6 c7 c8 (n 1)
2 2 2

an2 bn c a quadratic function of n

n n n
T (n) c1n c2 (n 1) c4 (n 1) c5 tj c6 tj 1 c7 tj 1 c8 (n 1)
j 2 j 2 j 2

while

t
n n n
n(n 1) n(n 1) n(n 1)
j j 1 ( j 1)
j 1 2 j 2 2 j 2 2

n(n 1) n(n 1) n(n 1)


T (n) c1n c2 (n 1) c4 (n 1) c5 1 c6 c7 c8 (n 1)
2 2 2

an2 bn c

n n n
T ( n) c1n c2 (n 1) c4 (n 1) c5 tj c6 tj 1 c7 tj 1 c8 (n 1)
j 2 j 2 j 2
Insertion Sort Overview
Advantage of Insertion Sort is that it
is relatively simple and easy to
implement.

Disadvantage of Insertion Sort is


that it is not efficient to operate with
a large list or input size.

You might also like