Академический Документы
Профессиональный Документы
Культура Документы
11/14/2014
3.
11/14/2014
subproblem 1
of size n/2
subproblem 2
of size n/2
a solution to
subproblem 1
a solution to
subproblem 2
11/14/2014
a solution to
the original problem
11/14/2014
k
Efficiency: (for n = 2 )
11/14/2014
11/14/2014
11/14/2014
Mergesort
Algorithm:
Divide: Divide array A[0..n-1] in two and make copies of each half in
arrays B[0.. n/2 - 1] and C[0.. n/2 - 1]
Conquer: Sort arrays B and C recursively using merge sort
11/14/2014
11/14/2014
Mergesort Examples
83297154
Efficiency?
Worst case: the smaller element comes from alternating arrays.
11/14/2014
10
11/14/2014
11
Quicksort
11/14/2014
A[i]p
A[i] p
12
11/14/2014
13
11/14/2014
14
Quicksort Example
5 3 1 9 8 2 4 7
Exercise:
15 22 13 27 12 10 20 25
11/14/2014
15
11/14/2014
Efficiency of Quicksort
Based on whether the partitioning is balanced.
Best case: split in the middle ( n log n)
C(1)=0
C(n) = 2C(n/2) + n
16
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
11/14/2014
17
18
11/14/2014
Efficiency
C(n) ( log n)
11/14/2014
19
Definitions
Example:
11/14/2014
20
Efficiency?
11/14/2014
11/14/2014
Preorder(TR)
How
11/14/2014
22
c00 c01
a00 a01
b00 b01
c10 c11
a10 a11
b10 b11
8 multiplications
4 additions
11/14/2014
23
c10 c11
a00 a01
a10 a11
m1
m2
m3
m4
m5
m6
m11/14/2014
7 = (a01 - a11) * (b10 + b11)
=
=
=
=
=
=
b00 b01
b10 b11
m1 + m4 - m5 + m7
m2 + m4
m3 + m5
m1 + m3 - m2 + m6
7 multiplications
18 additions
24
11/14/2014
A00 A01
=
C10 C11
B00 B01
*
A10 A11
B10 B11
M1 + M 4 - M5 + M 7
M3 + M 5
=
M2 + M 4
M1 + M 3 - M2 + M 6
11/14/2014
25
Submatrices:
11/14/2014
26
Efficiency, M(n)=n2.807
Addition operation
Efficiency, A(n)=n2.807
11/14/2014
27