Merge Sort
Merge Sort
Algorithms
CSE 246
Sorting
• Insertion sort
– Design approach: incrementa
– Sorts in place: lYes
– Best case: Θ(n)
– Worst case: Θ(n2)
• Bubble Sort
– Design approach: incrementa
– Sorts in place: lYes
– Running time: Θ(n2)
2
Sorting
• Selection sort
– Design approach: incrementa
– Sorts in place: lYes
– Running time: Θ(n2)
• Merge Sort
– Design approach: divide and
– Sorts in place: conquer
No
– Running time: Let’s
see!!
3
Divide-and-Conquer
• Divide the problem into a number of sub-problems
– Similar sub-problems of smaller size
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
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
6
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
7
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
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
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
9
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
10
Merging
p q r
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
11
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]
12
Example: MERGE(A, 9, 12, 16)
p q r
separate
memory
replace in
main
array
13
Example: MERGE(A, 9, 12, 16)
14
Example (cont.)
15
Example (cont.)
16
Example (cont.)
Done!
17
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 n n
L[1 . . n1 + 1] and the next n2 elements1 into R[1 . 2.
n2 + 1] p q
3. L[n1 + 1] ← ∞; R[n2 + 1] ← ∞ L 2 4 5 7 ∞
q+1 r
4. i ← 1; j ← 1
R 1 2 3 6 ∞
5. for k ← p to r
6. do if L[ i ] ≤ R[ j ]
7. then A[k] ← L[ i ]
8. i ←i + 1
9. else A[k] ← R[ j ]
18
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)
19
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
20
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
21
Solve the Recurrence
T(n) = c if n =
1
2T(n/2) + cn if n >
1
22