Professional Documents
Culture Documents
Lecture 4
Lecture 4
Merge Sort. The idea of the Merge Sort is to split a list into two halves, recursively sort
each half, then merge the two sorted sublists.
procedure merge sort(a[1, ....n])
Input: An array of numbers a[1, ..., n]
Output: A sorted version of this array
if n > 1 then
merge merge sort(a[1, ..., bn/2c]), merge sort(a[bn/2c + 1, ..., n])
To merge two ordered lists, we can use the following algorithm for the subroutine merge
used in the merge sort algorithm.
procedure merge(L1 , L2 : sorted lists)
L := empty list
while L1 and L2 are both nonempty
(remove smallest between first elements of L1 and L2 , place it at the right end of L)
if (this removal makes a list empty) then (remove all elements from other list and append
them to L)
return L
(L is the merged list with elements in increasing order).
Example. Merge the two lists 2, 3, 5, 6 and 1, 4.
Solution.
First List Second List Merged List
2356
14
2356
4
1
356
4
12
4
123
56
56
1234
123456
Two sorted lists with m and n elements respectively can be merged into a sorted list using
no more than m + n 1 comparisons.
The number of comparisons in the worst case for an array of n elements in the merge sort
is
T (n) = T bn/2c + T dn/2e + n 1
where n 1 is the maximum number of comparisons for merging. This has complexity
(n log n).
We will see how to solve a version of this under the assumption that n is a power of 2:
(
1
if n = 1
T (n) =
2 T (n/2) + n if n = 2k , k 1
Theorem 1 (The Master Theorem (First Part)). Let a > 1 and b > 1 be constants, and
let f (n) be a function. Suppose that T (n) is defined on the nonnegative integers by the
recurrence
T (n) = a T (n/b) + f (n)
If f (n) is (nlogb a ), then T (n) is (nlogb a log n).
With a = 2, b = 2, and f (n) = n, we have nlogb a = nlog2 2 = n. Since f (n) is (nlogb a ),
T (n) is (n log n) by the Master Theorem.
Example. Find the complexity of T (n) = 4 T (n/2) + n2 .
Solution. a = 4, b = 2, and logb a = log2 4 = 2. Since f (n) = n2 and nlog2 4 = n2 , we know
f (n) is (nlog2 4 ). Thus, T (n) is (n2 log n).
Quick Sort. The idea of Quick Sort is to pick an element in the array to serve as a pivot.
Split the array into two parts: one with elements larger than the pivot, and the other with
elements smaller than the pivot. Recursively repeat the algorithm for both halves of the
original array.
Example. Sort the list 4, 2, 3, 7, 9, 1, 6
Solution. We use the first element of the array as the pivot. Choosing 4 as the pivot, we
obtain the sublists 2, 3, 1 and 7, 9, 6. Using 2 as the pivot for the first sublist gives the sublists
1 and 3, and using 7 as the pivot for the second sublist gives the sublists 6 and 9. Altogether,
we have
1 2 3
4
6 7 9
Suppose that the list is already in the correct order. Then using the first element of a sublist
as the pivot will create a sublist containing all elements but the first one. This means that
the maximum number of comparisons for quick sort on n elements is
(n 1) + (n 2) + + 1 =
n(n 1)
2
Theorem 2 (Master Theorem). a > 1 and b > 1 are constants, f (n) is a function, T (n) is
defined on the nonnegative integers by the recurrence T (n) = aT (n/b) + f (n).
i) If f (n) is O(nlogb a ) for some constant > 0, then T (n) is (nlogb a ).
ii) If f (n) is (nlogb a ), then T (n) is (nlogb a log n).
iii) If f (n) is (nlogb a+ ) for some constant > 0, and if af (n/b)
cf (n) for some
constant c < 1 and all sufficiently large n, then T (n) is f (n) .
Example. Let T (n) = 9T (n/3) + n. Here, f (n) = n, a = 9, and b = 3. Then nlogb a =
nlog2 9 = n2 . Since f (n) = n is O(n2 ), we can apply the first part of the Master Theorem:
T (n) is (n2 ).
Example. Let T (n) = T (2n/3) + 1. Here, f (n) = 1, a = 1, and b = 3/2. Then nlogb a =
nlog3/2 1 = 0. Since f (n) = 1 is (n0 ), we can apply the second part of the Master Theorem:
T (n) is (log n).
Example. Let T (n) = 4T (n/2) + n3 . Here, f (n) = n3 , a = 4, and b = 2. Then nlogb a =
nlog2 4 = n2 . Since f (n) = n3 is (n3+ ) and
4(n/2)3 = (1/2)n3
we can apply the first part of the Master Theorem: T (n) is (n3 ).
Definition (First-Order Recurrences). A first-order recurrence is a relation of the form
(
d
if n = k
T (n) =
cT (n 1) + f (n) if n > k
= 2 1
Definition (Second-Order Recurrences). A second-order recurrence is a relation of the form
if n = k
d1
T (n) = d2
if n = k + 1
c T (n 1) + c T (n 2) + f (n) if n > k
1
2
If f (n) = 0, then we say the recurrence is homogeneous.
An example of a second-order recurrence: Fibonacci numbers.
To solve a homogeneous second-order recurrence, we take
T (n) = c1 T (n 1) + c2 T (n 2)
and obtain the equation
T (n) c1 T (n 1) c2 T (n 2) = 0
We then consider the equation
x 2 c1 x c2
called the characteristic equation, and find its roots x1 and x2 . Then,
i) if x1 6= x2 , the general solution is
T (n) = Ax1 n + Bx2 n
ii) if x1 = x2 , the general solution is
T (n) = Ax1 n + Bnx2 n
1 5
1+ 5
A+
B=1
f1 =
2
2
Multiplying the first of the two previous equations by 1+2 5 , we obtain the new system of
equations
(
1+ 5
1+ 5
A
+
B=0
2
2
1+ 5
1 5
A+ 2 B =1
2
Subtracting the above second equation from the first gives
1 5
1+ 5
B
B = 1
2
2
1
This can be solved to find B =
. Then A = 15 , so we have
5
!n
!n
1
1+ 5
1
1 5
fn =
2
2
5
5
Example. Solve the recurrence T (n) = 4T (n 1) 4T (n 2) if n 2, T (0) = 1, and
T (1) = 0.
Solution. The characteristic equation is x2 4x 4 = (x 2)2 = 0, so the characteristic
roots are x1 = x2 = 2. Then T (n) = A(2n ) + Bn(2n ).
T (0) = A = 1
T (1) = 2A + 2B = 0
We find that B = 1, so T (n) = 2 n2n .
n