0% found this document useful (0 votes)
18 views14 pages

Merge Sort: Basic Step: merge 2 sorted lists of total length n in Θ (n)

Merge sort is a divide and conquer algorithm that works by recursively splitting an array into two halves, sorting each half, and then merging the two sorted halves back together. It has a worst case time complexity of O(n log n) which is faster than insertion sort. The algorithm uses recursion to divide the problem into subproblems of sorting each half of the array, it conquers the subproblems by recursively sorting each half, and combines the solutions by merging the two sorted halves. Quicksort also uses a divide and conquer approach but partitions the array around a pivot element in each recursive call.

Uploaded by

Rohit Chaudhary
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views14 pages

Merge Sort: Basic Step: merge 2 sorted lists of total length n in Θ (n)

Merge sort is a divide and conquer algorithm that works by recursively splitting an array into two halves, sorting each half, and then merging the two sorted halves back together. It has a worst case time complexity of O(n log n) which is faster than insertion sort. The algorithm uses recursion to divide the problem into subproblems of sorting each half of the array, it conquers the subproblems by recursively sorting each half, and combines the solutions by merging the two sorted halves. Quicksort also uses a divide and conquer approach but partitions the array around a pivot element in each recursive call.

Uploaded by

Rohit Chaudhary
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Merge Sort

 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;
 pr
Merge-Sort(A, p, 2  );
 pr
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)

T(n/2) T(n/2) Θ(n/2) Θ(n/2)

T(n/4) T(n/4) T(n/4) T(n/4)


Recursion Tree (cont)
Θ(n) Θ(n)

Θ(n) Θ(n/2) Θ(n/2)

Θ(n) Θ(n/4) Θ(n/4) Θ(n/4) Θ(n/4) h=lg n

Θ(n) Θ(n/8) Θ(n/8) Θ(n/8) Θ(n/8) Θ(n/8) Θ(n/8) Θ(n/8) Θ(n/8)

Θ(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
qpartition(A,p,r);
Quicksort(A,p,q);
Quicksort(A,q+1,r);
}
Partition procedure
p r

Partition(A, p, r)
{ xA[p]; ip-1; jr+1; i j

while TRUE do
{ repeat jj-1 until A[j]≤x;
repeat ii+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?

 Average case: Θ(n lg n), assuming all per


mutations equally likely.
(Will be proved later.)

You might also like