Académique Documents
Professionnel Documents
Culture Documents
a problem of size n
subproblem 1
of size n/2
subproblem 2
of size n/2
a solution to
subproblem 1
a solution to
subproblem 2
a solution to
the original problem
Divde_Conquer recurrence
relation
The computing time of Divde_Conquer is
n small
g ( n)
T ( n)
T ( n1 ) T ( n2 ) ... T ( nk ) f ( n) otherwise
Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.
Example: Find 9
3
12
15
Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.
Example: Find 9
Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.
Example: Find 9
Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.
Example: Find 9
Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.
Example: Find 9
Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.
Example: Find 9
Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.
Example: Find 9
b=2, a=1
Case 2 (k=0)
cost of dividing
and combining
QuickSort
We use Divide-and-Conquer:
1. Divide: partition A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such
that each element of A[p..q-1] is A[q], and each element of A[q+1..r] is
A[q]. Compute q as part of this partitioning.
1. Conquer: sort the subarrays A[p..q-1] and A[q+1..r] by recursive calls to
QUICKSORT.
1. Combine: the partitioning and recursive sorting leave us with a sorted
A[p..r] no work needed here.
An obvious difference is that we do most of the work in the divide stage, with
no work at the combine one.
Quicksort Algorithm
Quicksort example
2
Current pivots
Previous pivots
Quicksort
2
Hi, I am nothing
Nothing Jr.
Nothing 3rd
QuickSort
The Pseudo-Code
QuickSort
QuickSort
Proof of Correctness: PARTITION
We look for a loop invariant and we observe that at the
beginning of each iteration of the loop (l.3-6) for any
array index k:
QuickSort
The Invariant
2. A[j]x.
Analysis
T(N) = T(i) + T(N - i -1) + cN
The time to sort the file is equal to
the time to sort the left partition with i elements, plus
the time to sort the right partition with N-i-1 elements, plus
the time to build the partitions.
23
Worst-Case Analysis
The pivot is the smallest (or the largest) element
T(N) = T(N-1) + cN, N > 1
24
Best-Case Analysis
Divide by N:
T(N) / N = T(N/2) / (N/2) + c
25
26
Worst-Case Analysis
What will be the worst case?
The pivot is the smallest element, all the time
Partition is always unbalanced
Best-case Analysis
What will be the best case?
Partition is perfectly balanced.
Pivot is always in the middle (median of the array)