02 D&C Merge and Quicksort
02 D&C Merge and Quicksort
Approach…..
1
Recurrences
• A recurrence is an equation or inequality that
describes a function in of its value on smaller
inputs.
The divide-and-conquer approach
• Recursive in structure
Steps:
1. DIVIDE problem into smaller number of
subproblems
2. CONQUER by finding solution to
subproblems (recursive)
3. COMBINE solutions of subproblems to find
solution of original problem
Case Study
• Find different ways to sort ‘n’ numbers……
• Strategy #1 : Insertion sort….
Insertion Sort- Recursive Version
• Array divided in two parts
Sorted unsorted
Insert_sort(A, start=2, n)
6
Recurrence Relation
• 𝑇(𝑛), time to run insert_sort on length ‘n’
• Time 𝑇(𝑛 − 1) to sort A[0],….,A[n-2]
• 𝑇(𝑛) = 1 + 2 + 𝑇(𝑛 − 2)
• 𝑇(𝑛) = 1 + 2 + 3 + ⋯ + 𝑛 − 1 + 𝑇(1)
• 𝑇(𝑛) = 𝑛 (𝑛 − 1)/2 = 𝑂(𝑛2 )
7
Clever sorting
• Divide the array in two equal parts
• Separately sort each part
• Combine the results
Strategy #2
• Given two sorted arrays A[m], B[n] (non empty)
• Let the pointers to these array be ‘a’ and ‘b’
respectively
• Initialize a new array C[m+n], and pointer ‘c’
• At any pointer positions ‘a’ and ‘b’
• Compare:
– If A[a] < B[b] then C[c]=A[a], increment ‘a’
– Else C[c]=B[b], increment ‘b’
9
Strategy#2
• Example
• A=[ 4 12 40]
• B=[ 3 9 24]
• Then C=[ ]?
10
Merge Sort
• Divide array into two equal parts
• Sort left part
• Sort right part
• Merge two sorted halves
12
Merge Sort
Algorithm for MergeSort
Algorithm MergeSort (A, left, right, B)
1. If right-left > 0
2. mid= (𝑙𝑒𝑓𝑡 + 𝑟𝑖𝑔ℎ𝑡)/2
3. Let L[1….. m] and R[1….n] be two new arrays where
m=(mid-left)+1, and n=(right-(mid+1)+1)=(right-mid)
4. Mergesort(A, left, mid, L)
5. Mergesort (A, mid+1, right, R)
6. B = Merge (L, m, R, n)
Subroutine Merge
Algorithm Merge (L, m, R, n)
1. Initialize i=1, j=1, k=1
2. Let C[1,…..m+n] be a new array
3. While k≤m+n
4. if L[i]< R[j] or j=n
5. C[k]=L[i]
6. increment i=i+1, k=k+1
7. if R[j]≤L[i]
8. C[k]=R[j]
9. increment j=j+1, k=k+1
10. Return C
MergeSort Analysis
• Analysis of Merge(A,m1,B,n1)
• For each comparison between A[i] and B[j] one element gets
filled in C[k]
• How many elements? Ans n = (m1+n1)
• Total number of comparison atmost: n
• Thus complexity of Merge ()-> 𝜽(𝒏)
16
MergeSort Analysis
• Merge sort on just one element takes constant time.
• When we have n>1 elements, we break down the running
time as follows
17
MergeSort Analysis
• Suppose that our division of the problem yields a
subproblems, each of which is 1/b the size of the
original
𝑐, 𝑖𝑓 𝑛 = 1
• 𝑇(𝑛) = ቐ 𝑛
2𝑇 + 𝑐𝑛, 𝑖𝑓 𝑛 > 1
2
• where the constant c represents the time required to solve
problems of size 1
MergeSort Analysis
• Therefore: T(n)=2T(n/2)+cn
20
MergeSort Analysis
• Therefore: T(n)=2T(n/2)+cn
21
MergeSort Analysis
𝑛
• 𝑇 𝑛 = 2𝑇 + 𝑐𝑛
2
𝑛 𝑛 𝑐𝑛
But we know that….𝑇 2
= 2𝑇 4
+ 2
𝑛 𝑐𝑛 𝑛
• 𝑇 𝑛 = 2[2𝑇 + ] + 𝑐𝑛= 22𝑇 + 2𝑐𝑛
22 2 22
𝑛 𝑛 𝑐𝑛
But we know that….𝑇 = 2𝑇 +
4 8 4
𝑛 𝑐𝑛 𝑛
• 𝑇 𝑛 = 22[2𝑇 + ] + 2𝑐𝑛= 23𝑇 + 3𝑐𝑛
23 4 23
• after some iterations…we can write
MergeSort Analysis
• after some iterations…we can write
𝑛
• 𝑇 𝑛 = 2k𝑇 + 𝑘𝑐𝑛
2𝑘
• Let the base case occur when T(1)=c
𝑛
• For that = 1, which mean 𝑛= 2𝑘, or k=log2 n
2𝑘
• Thus
• 𝑇 𝑛 = 𝑛 𝑐 + 𝑐 𝑛 𝑙𝑜𝑔2 𝑛= 𝜃(𝑛 log 𝑛)
Recursion Tree
MergeSort Analysis
• Does order of input elements have any effect
on worst case or best case???
Tutorial Question
1. Although merge sort runs in 𝜃 (n lg n) worst-case time and insertion sort runs in 𝜃 (n2)
worst-case time, the constant factors in insertion sort can make it faster in practice for
small problem sizes on many machines. Thus, it makes sense to coarsen the leaves of the
recursion by using insertion sort within merge sort when subproblems become
sufficiently small. Consider a modification to merge sort in which n/k sublists of length
k are sorted using insertion sort and then merged using the standard merging mechanism,
where k is a value to be determined.
• a. Show that insertion sort can sort the n/k sublists, each of length k, in in 𝜃 (nk)
worst-case time.
• b. Show how to merge the sublists in 𝜃 (n lg (n/k)) worst-case time.
• c. Given that the modified algorithm runs in in 𝜃 (nk+ n lg (n/k)) worst-case time,
what is the largest value of k as a function of n for which the modified algorithm
has the same running time as standard merge sort, in terms of ‚ 𝜃 -notation?
(page 40 CLRS)
Tutorial Question
• a. Show that insertion sort can sort the n/k sublists, each of length k, in in 𝜃 (nk)
worst-case time.
Tutorial Question
• b. Show how to merge the sublists in 𝜃 (n lg (n/k)) worst-case time.
Tutorial Question
• c. Given that the modified algorithm runs in in 𝜃 (nk+ n lg (n/k)) worst-case time,
what is the largest value of k as a function of n for which the modified algorithm
has the same running time as standard merge sort, in terms of ‚ 𝜃 -notation?
(page 40 CLRS)
Tutorial Question
• Observe that the while loop of lines 5–7 of the
INSERTION-SORT procedure uses a linear
search to scan (backward) through the sorted
subarray A[1,…, j-1].
• Can we use a binary search (see Exercise 2.3-
5) instead to improve the overall worst-case
running time of insertion sort to O(nlog n)?
Sorting Strategy #3
Partition about the Pivot
• Given an array: 5 3 7 18 24 4 8
• Assume a pivot: 5 3 7 18 24 4 8
33
Two Cool Facts about Partitioning
• Implemented in linear time, and without any extra memory space
needed.
• Reduces problem size to enable Divide & Conquer.
• Divide: 1 2 3 4 5 6 7
Original Array A[]
5 3 7 18 24 4 8
Partition (A[], First=1, Last=7)
1 2 3 4 5 6 7
5 3 7 4 8 18 24
Partition (A[], First=1, Last=4)
1 2 3 4
Partition (A[], First=6, Last=7)
6 7
3 4 5 7 18 24
Partition (A[], First=3, Last=4)
5 7
• Conquer:
3 4 5 7 8 18 24
High Level Description
Algorithm QUICKSORT(A, First, Last)
// initial call QUICKSORT(A,1,N)
1. If (First<Last)
2. P =Partition (A, First, Last) // P is pivot position
3. QUICKSORT(A, First, P-1)
4. QUICKSORT(A, P+1, Last)
LP RP RP
LP
8 42 38 29
75 77
LP RP
29 38 42
38