Merge Sort: Basic Step: merge 2 sorted lists of total length n in Θ (n)
Merge Sort: Basic Step: merge 2 sorted lists of total length n in Θ (n)
Basic Step:
merge 2 sorted lists of total length n in Θ
(n).
2 3 6 7 1 4 5 8
Recursive algorithm
Merge-Sort(A, p, r)
{ if p=r then return;
pr
Merge-Sort(A, p, 2 );
pr
Merge-Sort(A, 2 +1,r);
Merge results and return;
}
Recurrence
Describes a function recursively in terms o
f itself.
(1) , if n 1
T ( n)
2T (n / 2) (n) , if n 1
T(n)=?
Asymptotic notation
Big-oh notation:
f(n)=O(g(n)) if ∃ const c, n0 s.t. 0 ≤ f(n) ≤ cg
(n), ∀n ≥ n0.
eg. 2n2=O(n3). (c=1, n0=2)
O(g(n)) is a set of functions:
{f(n): ∃ const c, n0 s.t. 0 ≤ f(n) ≤ cg(n), ∀n ≥ n
0}
2n2∈O(n3).
O-notation is an upper-bound notation.
Asymptotic notation (cont.)
Ω-notation (Lower bounds)
Ω(g(n))={f(n): ∃ const c, n0 s.t. 0 ≤ cg(n) ≤ f(n)
, ∀n ≥ n0}
eg. n =Ω(lg n)
Θ-notation
Θ(g(n))={f(n): ∃ const c1, c2, n0 s.t.
0 ≤ c1g(n) ≤ f(n) ≤ c2g(n),
∀n ≥ n0}
Recursion Tree
T(n)=2T(n/2)+Θ(n)
Θ(n) Θ(n)
Θ(n) Θ(1)
(lg n)Θ(n)=Θ(n lg n). Θ(n lg n) grows more slowly th
an Θ(n2). Mergesort asymtotically beats insertion so
rt in the worst case.
Divide and Conquer
Divide a problem into subproblems.
Conquer subproblems by solving recursive
ly.
Combine subproblem solutions.
eg. Mergesort
Divide:
trivial.
Conquer: sort 2 subarrays.
Combine: linear time merge.
Divide and Conquer (cont.)
eg. Binary search of a sorted array.
Divide:check the middle element
Conquer: search 1 subarray
Combine: trivial
Time complexity: T(n)=T(n/2)+Θ(1)=Θ(lg n).
Quicksort (7.1, 7.2)
Divide and conquer:
Divide:partition array into 2 subarrays s.t.
elements in lower parts ≤ eleme
nts in higher parts.
Conquer: recursively sort 2 subarrays.
Combine: trivial.
Quicksort algorithm
Quicksort(A, p, r)
{ if p < r then
qpartition(A,p,r);
Quicksort(A,p,q);
Quicksort(A,q+1,r);
}
Partition procedure
p r
Partition(A, p, r)
{ xA[p]; ip-1; jr+1; i j
while TRUE do
{ repeat jj-1 until A[j]≤x;
repeat ii+1 until A[i]≥x;
if i<j then exchange A[i] and A[j]
else return j;
}
}
Analysis of quicksort
Assume all input elements distinct.
If lucky, Partition splits the array evenly. T(n)=2T
(n/2)+Θ(n)=Θ(n lg n).
If unlucky, one side of partition has no element.
T(n)=T(0)+T(n-1)+Θ(n)
=T(n-1)+Θ(n)
=T(n-2)+Θ(n-1)+Θ(n)
………
=Θ(1)+Θ(2)+…+Θ(n-1)+Θ(n)
=Θ(n2)
Analysis of quicksort (cont.)
When does the worst case occur?