2 Mergesort
2 Mergesort
CSOR W4246
Eleni Drinea
Computer Science Department
Columbia University
1 Asymptotic notation
1 Asymptotic notation
Definition 1 (O).
We say that T (n) = O(f (n)) if there exist constants c > 0 and
n0 ≥ 0 s.t. for all n ≥ n0 , we have T (n) ≤ c · f (n) .
T(n) = O(f(n))
c f(n)
T(n)
n
n0
Asymptotic upper bounds: Big-O notation
Definition 1 (O).
We say that T (n) = O(f (n)) if there exist constants c > 0 and
n0 ≥ 0 s.t. for all n ≥ n0 , we have T (n) ≤ c · f (n) .
Definition 2 (Ω).
We say that T (n) = Ω(f (n)) if there exist constants c > 0 and
n0 ≥ 0 s.t. for all n ≥ n0 , we have T (n) ≥ c · f (n).
T(n) = Ω(f(n))
T(n)
c f(n)
n
n0
Asymptotic lower bounds: Big-Ω notation
Definition 2 (Ω).
We say that T (n) = Ω(f (n)) if there exist constants c > 0 and
n0 ≥ 0 s.t. for all n ≥ n0 , we have T (n) ≥ c · f (n).
c2 f(n)
T(n)
T(n) = ϴ(f(n))
c1 f(n)
n
n0
Asymptotic tight bounds: Θ notation
Definition 3 (Θ).
We say that T (n) = Θ(f (n)) if there exist constants c1 , c2 > 0
and n0 ≥ 0 s.t. for all n ≥ n0 , we have
Equivalent definition
T (n) = Θ(f (n)) if T (n) = O(f (n)) and T (n) = Ω(f (n))
Asymptotic tight bounds: Θ notation
Definition 3 (Θ).
We say that T (n) = Θ(f (n)) if there exist constants c1 , c2 > 0
and n0 ≥ 0 s.t. for all n ≥ n0 , we have
Equivalent definition
T (n) = Θ(f (n)) if T (n) = O(f (n)) and T (n) = Ω(f (n))
Definition 4 (o).
We say that T (n) = o(f (n)) if, for any constant c > 0, there
exists a constant n0 ≥ 0 such that for all n ≥ n0 , we have
T (n) < c · f (n) .
Asymptotic upper bounds that are not tight: little-o
Definition 4 (o).
We say that T (n) = o(f (n)) if, for any constant c > 0, there
exists a constant n0 ≥ 0 such that for all n ≥ n0 , we have
T (n) < c · f (n) .
Definition 4 (o).
We say that T (n) = o(f (n)) if, for any constant c > 0, there
exists a constant n0 ≥ 0 such that for all n ≥ n0 , we have
T (n) < c · f (n) .
Definition 5 (ω).
We say that T (n) = ω(f (n)) if, for any constant c > 0, there
exists a constant n0 ≥ 0 such that for all n ≥ n0 , we have
T (n) > c · f (n).
Asymptotic lower bounds that are not tight: little-ω
Definition 5 (ω).
We say that T (n) = ω(f (n)) if, for any constant c > 0, there
exists a constant n0 ≥ 0 such that for all n ≥ n0 , we have
T (n) > c · f (n).
Definition 5 (ω).
We say that T (n) = ω(f (n)) if, for any constant c > 0, there
exists a constant n0 ≥ 0 such that for all n ≥ n0 , we have
T (n) > c · f (n).
1. Transitivity
1.1 If f = O(g) and g = O(h), then f = O(h).
1.2 If f = Ω(g) and g = Ω(h), then f = Ω(h).
1.3 If f = Θ(g) and g = Θ(h), then f = Θ(h).
2. Sums of up to a constant number of functions
2.1 If f = O(h) and g = O(h), then f + g = O(h).
2.2 Let k be a fixed constant, and let f1 , f2 , . . . , fk , h be
functions such that for all i, fi = O(h). Then
f1 + f2 + . . . + fk = O(h).
3. Transpose symmetry
I f = O(g) if and only if g = Ω(f ).
I f = o(g) if and only if g = ω(f ).
Today
1 Asymptotic notation
Remarks
I Mergesort is a recursive procedure (why?)
I Initial call: Mergesort(A, 1, n)
I Subroutine Merge merges two sorted lists of sizes bn/2c, dn/2e
into one sorted list of size n. How can we accomplish this?
Merge: intuition
1. Correctness
2. Running time
3. Space
Analysis of Merge: correctness
3. Space
Merge: pseudocode
3. Space
Analysis of Merge: space
1. Correctness
2. Running time
3. Space
Mergesort: correctness
For simplicity, assume n = 2k for integer k ≥ 0.
We will use induction on k.
I Base case: For k = 0, the input consists of 1 item; Mergesort
returns the item.
I Induction Hypothesis: For k ≥ 0, assume that Mergesort
correctly sorts any list of size 2k .
I Induction Step: We will show that Mergesort correctly sorts
any list A of size 2k+1 .
From the pseudocode of Mergesort, we have:
ILine 3: mid takes the value 2k
ILine 4: Mergesort(A, 1, 2k ) correctly sorts the leftmost half
of the input, by the induction hypothesis.
I Line 5: Mergesort(A, 2k + 1, 2k+1 ) correctly sorts the
1 Asymptotic notation
O(nlogb a ) , if a > bk
1. T (n) = 2T (n − 1) + 1, T (1) = 2