Divide-and-Conquer Algorithm: Strategy, Technique, and Examples, Study notes of Digital & Analog Electronics

An overview of the divide-and-conquer algorithm strategy, its technique, and examples of its application in sorting algorithms such as mergesort and quicksort. The document also includes the master theorem and its application to the analysis of divide-and-conquer algorithms.

Typology: Study notes

2010/2011

Uploaded on 09/02/2011

hamit1990
hamit1990 🇮🇳

4.3

(76)

95 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Divide-and-Conquer
Divide-and-Conquer
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Divide-and-Conquer Algorithm: Strategy, Technique, and Examples and more Study notes Digital & Analog Electronics in PDF only on Docsity!

Divide-and-Conquer

Divide-and-Conquer

Divide-and-Conquer

The most-well known algorithm design

strategy:

1. Divide instance of problem into two or

more smaller instances

2. Solve smaller instances recursively

3. Obtain solution to original (larger)

instance by combining these solutions

Divide-and-Conquer

Examples

Sorting: mergesort and quicksort

General Divide-and-Conquer Recurrence

General Divide-and-Conquer Recurrence

T
T

n

n ) =

aT

aT (

n/b

n/b ) +

f

f (

n

n )

where

where f

f (

n

n )

n

n

d

d

d

d

Master Theorem

Master Theorem : If

: If a < b

a < b

d

d

T
T

n

n )

n

n

d

d

If

If a = b

a = b

dd

T
T

n

n )

n

n

dd

log

log n

n )

If

If a > b

a > b

dd

T
T

n

n )

n

n

log

log

b

b

a

a

Note: The same results hold with O instead of

Note: The same results hold with O instead of 

Examples:

Examples: T

T

n

n

) = 4

T
T

n

n

/2) +

n

n

T
T

n

n

)

T
T

n

n ) = 4

T
T

n

n /2) +

n

n

2

2

T
T

n

n )

T
T

n

n ) = 4

T
T

n

n /2) +

n

n

33

T
T

n

n )

n^

n^ )

n^2log n

n^2log n )

n^

n^ )

Pseudocode of Mergesort

Pseudocode of Merge

Time complexity: Θ

p+q

p+q )

n

n )

) comparisons

Analysis of Mergesort

  • All cases have same efficiency: Θ( n log n )
  • Number of comparisons in the worst case is

close to theoretical minimum for comparison-

based sorting:

 log

2

n ! ≈ n log

2

n - 1.44 n

  • Space requirement: Θ( n ) (not in-place)
  • Can be implemented without recursion

(bottom-up)

T(n) = 2T(n/2) +

T(n) = 2T(n/2) + Θ

(n), T(1) = 0

(n), T(1) = 0

Quicksort

Select a pivot (partitioning element) – here, the

first element

Rearrange the list so that all the elements in the

first s positions are smaller than or equal to the

pivot and all the elements in the remaining n-s

positions are larger than or equal to the pivot

(see next slide for an algorithm)

Exchange the pivot with the last element in the

first (i.e., ) subarray — the pivot is now in its

final position

Sort the two subarrays recursively

p

A[ i ] p

A[ i ] p

Quicksort Example

5 3 1 9 8 2 4 7

Analysis of Quicksort

Best case: split in the middle — Θ( n log n )

Worst case: sorted array! — Θ( n

2

Average case: random arrays — Θ( n log n )

Improvements:

better pivot selection: median of three partitioning

  • switch to insertion sort on small subfiles
  • elimination of recursion

These combine to 20-25% improvement

Considered the method of choice for

internal sorting of large files ( n ≥ 10000)