Module 2A Merge SOrt and Quick Sort
Module 2A Merge SOrt and Quick Sort
Module Objectives
Discuss Key Concepts of Divide n Conquer Approach
2
Contents
3
Amity School of Engineering & Technology
Learning Outcomes
Students will be able to
4
Introduction
5
Merge Sort Approach
• To sort an array A[p . . r]:
• Divide
• Divide the n-element sequence to be sorted into two subsequences of n/2
elements each
• Conquer
• Sort the subsequences recursively using merge sort
• When the size of the sequences is 1 there is nothing more to do
• Combine
• Merge the two sorted subsequences
6
Mergesort
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Alg.: MERGE-SORT(A, p, r) 5 2 4 7 1 3 2 6
MERGE-SORT(A, p, q) Conquer
MERGE-SORT(A, q + 1, r) Conquer
MERGE(A, p, q, r) Combine
8
Example – n Power of 2
1 2 3 4 5 6 7 8
Divide 5 2 4 7 1 3 2 6 q=4
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
9
Example – n Power of 2
1 2 3 4 5 6 7 8
Conquer 1 2 2 3 4 5 6 7
and
Merge 1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
10
Example – n Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6 q=6
Divide
1 2 3 4 5 6 7 8 9 10 11
q=3 4 7 2 6 1 4 7 3 5 2 6 q=9
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6
1 2 4 5 7 8
4 7 6 1 7 3
11
Example – n Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11
Conquer 1 2 2 3 4 4 5 6 6 7 7
and
Merge
1 2 3 4 5 6 7 8 9 10 11
1 2 4 4 6 7 2 3 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 4 7 1 4 6 3 5 7 2 6
1 2 3 4 5 6 7 8 9 10 11
4 7 2 1 6 4 3 7 5 2 6
1 2 4 5 7 8
4 7 6 1 7 3
12
Merging
p q r
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
13
Merging
p q r
• Idea for merging: 1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
– Two piles of sorted cards
• Choose the smaller of the two top cards
• Remove it and place it in the output pile
A2 A[q+1, r]
14
Example: MERGE(A, 9, 12, 16)
p q r
15
Example: MERGE(A, 9, 12, 16)
16
Example (cont.)
17
Example (cont.)
18
Example (cont.)
Done!
19
Merge - Pseudocode
p q r
Alg.: MERGE(A, p, q, r) 1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
1. Compute n1 and n2
2. Copy the first n1 elements into n1 n2
L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]
3. L[n1 + 1] ← ; R[n2 + 1] ← p q
4. i ← 1; j ← 1 L 2 4 5 7
q+1 r
5. for k ← p to r
R 1 2 3 6
6. do if L[ i ] ≤ R[ j ]
7. then A[k] ← L[ i ]
8. i ←i + 1
9. else A[k] ← R[ j ]
10. j←j+1 20
Running Time of Merge
(assume last for loop)
• Initialization (copying into temporary arrays):
(n1 + n2) = (n)
• Adding the elements to the final array:
- n iterations, each taking constant time (n)
• Total time for Merge:
(n)
21
Analyzing Divide-and Conquer Algorithms
T(n)= (1) if n ≤ c ;
aT(n/b) + D(n) + C(n) otherwise
22
MERGE-SORT Running Time
• Divide:
– compute q as the average of p and r: D(n) = (1)
• Conquer:
– recursively solve 2 subproblems, each of size n/2 2T
(n/2)
• Combine:
– MERGE on an n-element subarray takes (n) time
C(n) = (n)
(1) if n =1
T(n) = 2T(n/2) + (n) if n > 1
23
Solve the Recurrence
T(n) = c if n = 1
2T(n/2) + cn if n > 1
24
Merge Sort - Discussion
• Advantages:
– Guaranteed to run in (nlgn)
• Disadvantage
– Requires extra space N
25
Test your Skills…
26
Summary
27
Summary
28
Quicksort
A[p…q] ≤ A[q+1…r]
29
Quicksort
A[p…q] ≤ A[q+1…r]
• Conquer
– Recursively sort A[p..q] and A[q+1..r] using
Quicksort
• Combine
– Trivial: the arrays are sorted in place
– No additional work is required to combine them
– The entire array is now sorted
30
QUICKSORT
if p < r
then q PARTITION(A, p, r)
QUICKSORT (A, p, q)
31
Partitioning the Array
• Choosing PARTITION()
– There are different ways to do this
x A[j…r]
i j
32
Example
A[p…r] pivot x=5
5 3 2 6 4 1 3 7 5 3 2 6 4 1 3 7
i j i j
3 3 2 6 4 1 5 7 3 3 2 6 4 1 5 7
i j i j
A[p…q] A[q+1…r]
3 3 2 1 4 6 5 7 3 3 2 1 4 6 5 7
i j j i
33
Example
34
Partitioning the Array
Alg. PARTITION (A, p, r)
p r
1. x A[p]
A: 5 3 2 6 4 1 3 7
2. i p – 1
3. j r + 1 i j
A[p…q] ≤ A[q+1…r]
4. while TRUE
5. do repeat j j – 1 A: ap ar
6. until A[j] ≤ x
j=q i
7. do repeat i i + 1
8. until A[i] ≥ x
Each element is
9. if i < j visited once!
10. then exchange A[i] A[j] Running time: (n)
n=r–p+1
11. else return j
35
Recurrence
if p < r
then q PARTITION(A, p, r)
QUICKSORT (A, p, q)
• Worst-case partitioning
– One region has one element and the other has n – 1 elements
– Maximally unbalanced
n n
• Recurrence: q=1 1 n-1 n
1 n-2 n-1
T(n) = T(1) + T(n – 1) + n, n-2
n 1 n-3
T(1) = (1) 1
2 3
T(n) = T(n – 1) + n 1 1 2
n
n k 1 (n) (n 2 ) (n 2 ) (n2)
= k 1
When does the worst case happen? 37
Best Case Partitioning
• Best-case partitioning
– Partitioning produces two regions of size n/2
• Recurrence: q=n/2
T(n) = 2T(n/2) + (n)
T(n) = (nlgn) (Master theorem)
38
Difference Between Worst and
Best
• 9-to-1 proportional split
Q(n) = Q(9n/10) + Q(n/10) + n
39
Test your Skills…
40
Summary
• Learned Quick Sort.
• Without recursion implementation of quicksort is complicated.
• Worst case running time of Quick Sort is O(n2)>> ɵ(nlogn) for large n.
• Average Case complexity of Quick Sort is ɵ(nlogn) to sort n items.
• Merge Sort requires an auxiliary array of size n. Since it requires 2n spaces
of sorting. It’s called not in-place sorting.
• Merge Sort requires O(nlogn) time comparison based sorting algorithm.
41
References
[1] https://medium.com/karuna-sehgal
[2] https://www.studytonight.com
42