Professional Documents
Culture Documents
Lecture #9&10
Divide-and-Conquer
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
Split array A[0..n-1] into about equal halves and make copies
of each half in arrays B and C
Sort arrays B and C recursively
Merge sorted arrays B and C into array A as follows:
Repeat the following until no elements remain in one of the
arrays:
compare the first elements in the remaining unprocessed
portions of the arrays
copy the smaller of the two into A, while incrementing
the index indicating the unprocessed portion of that
array
Once all elements in one of the arrays are processed, copy
the remaining unprocessed elements from the other array
into A.
Pseudocode of Mergesort
Pseudocode of Merge
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
The non-recursive
version of Mergesort
3 8 2 9 1 7 4 5 starts from merging
single elements into
2 3 8 9 1 4 5 7 sorted pairs.
1 2 3 4 5 7 8 9
Analysis of Mergesort
A[i]p A[i]p
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
Partitioning Algorithm
or i > r
<
or j = l
5 3 1 9 8 2 4 7
2 3 1 4 5 8 9 7
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
Analysis of Quicksort
Best case: split in the middle (n log n)
Worst case: sorted array! (n2) T(n) = T(n-1) + (n)
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
l 0; r n-1
while l r do
m (l+r)/2
if K = A[m] return m
else if K < A[m] r m-1
else l m+1
return -1
Analysis of Binary Search
Time efficiency
worst-case recurrence: Cw (n) = 1 + Cw( n/2 ), Cw (1) = 1
solution: Cw(n) = log2(n+1)
TL TR
A = 12345678901357986429 B = 87654321284820912836
2135 4014
Brute-force algorithm
c00 c01 a00 a01 b00 b01
= *
c10 c11 a10 a11 b10 b11
m1 + m4 - m5 + m7 m3 + m5
=
m2 + m4 m1 + m3 - m2 + m6
m1 = (a00 + a11) * (b00 + b11)
m2 = (a10 + a11) * b00
m3 = a00 * (b01 - b11)
m4 = a11 * (b10 - b00)
m5 = (a00 + a01) * b11
m6 = (a10 - a00) * (b00 + b01)
m7 = (a01 - a11) * (b10 + b11)
7 multiplications
18 additions
Strassens Matrix Multiplication
M1 + M4 - M5 + M7 M 3 + M5
=
M2 + M4 M1 + M3 - M2 + M6
Formulas for Strassens Algorithm
M1 = (A00 + A11) (B00 + B11)