05 Merge Sort
05 Merge Sort
Merge Sort
• Bubble Sort
– Design approach: incremental
– Sorts in place: Yes
– Running time: (n2)
CS 477/677 - Lecture 5 2
Sorting
• Selection sort
– Design approach: incremental
– Sorts in place: Yes
– Running time: (n2)
• Merge Sort
– Design approach: divide and conquer
– Sorts in place: No
– Running time: Let’s see!!
CS 477/677 - Lecture 5 3
Divide-and-Conquer
• Divide the problem into a number of subproblems
– Similar sub-problems of smaller size
CS 477/677 - Lecture 5 4
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
CS 477/677 - Lecture 5 5
Merge Sort
p q r
1 2 3 4 5 6 7 8
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
CS 477/677 - Lecture 5 6
Example – n Power of 2
1 2 3 4 5 6 7 8
Example 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
CS 477/677 - Lecture 5 7
Example – n Power of 2
1 2 3 4 5 6 7 8
1 2 2 3 4 5 6 7
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
CS 477/677 - Lecture 5 8
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
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
CS 477/677 - Lecture 5 9
Example – n Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11
1 2 2 3 4 4 5 6 6 7 7
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
CS 477/677 - Lecture 5 10
Merging
p q r
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
CS 477/677 - Lecture 5 11
Merging
CS 477/677 - Lecture 5 12
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
L 2 4 5 7
4. i ← 1; j ← 1
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 +CS1477/677 - Lecture 5 13
Analyzing Divide-and Conquer
Algorithms
• The recurrence is based on the three steps of
the paradigm:
– T(n) – running time on a problem of size n
– Divide the problem into a subproblems, each of size
n/b: takes D(n)
– Conquer (solve) the subproblems aT(n/b)
– Combine the solutions C(n)
(1) if n ≤ c
T(n) = aT(n/b) + D(n) + C(n) otherwise
CS 477/677 - Lecture 5 14
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
CS 477/677 - Lecture 5 15
Solve the Recurrence
T(n) = c if n = 1
2T(n/2) + cn if n > 1
CS 477/677 - Lecture 5 16