Merge Sort
Merge Sort
Merge Sort
Sort
Divide-and-Conquer
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
Problem statement:
Given two arrays A[0..N-1] and B[0..M-1]
sorted in place. Merge them to an external array
C[0..N+M-1]
Such that C[] is also sorted
1 2 3 4 10 20 5 16 22 43 50 67
A[0..5] B[0..5]
1 2 3 4 5 10 16 20 22 43 50 67
C[0..11]
Mergesort
• Split array A[0..n-1] into about equal halves and make copies
of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
Repeat the following until no elements remain in one of the
arrays:
• compare the first elements in the remaining unprocessed
portions of the arrays
• copy the smaller of the two into A, while incrementing
the index indicating the unprocessed portion of that
array
Once all elements in one of the arrays are processed, copy
the remaining unprocessed elements from the other array
into A.
Merging
i=0 J=0
1 2 3 4 10 20 5 16 22 43 50 67
K=0
1
Merging
i=1 J=0
1 2 3 4 10 20 5 16 22 43 50 67
K=1
1 2
Merging
i=2 J=0
1 2 3 4 10 20 5 16 22 43 50 67
K=2
1 2 3
Merging
i=3 J=0
1 2 3 4 10 20 5 16 22 43 50 67
K=3
1 2 3 4
Merging
i=4 J=0
1 2 3 4 10 20 5 16 22 43 50 67
K=4
1 2 3 4 5
Merging
i=4 J=1
1 2 3 4 10 20 5 16 22 43 50 67
K=5
1 2 3 4 5 10
Merging
i=5 J=1
1 2 3 4 10 20 5 16 22 43 50 67
K=6
1 2 3 4 5 10 16
Merging
i=5 J=2
1 2 3 4 10 20 5 16 22 43 50 67
K=7
1 2 3 4 5 10 16 20
Merging
i=6 J=2
1 2 3 4 10 20 5 16 22 43 50 67
K=6
1 2 3 4 5 10 16 20 22 43 50 67
Merging
Merge(A,B,n,m,C) :
Select A[i] and B[j] starting from i=0 and j=0 and k = 0
If A[i] < B[j]
C[k]= A[i]
increment k and i by 1
Else
C[k] = B[j]
increment k and j
If i=n and j<m
copy all remaining elements of B into C
If j=m and i<n
copy all remaining elements of A into C
Mergesort
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
Merge Sort Example
•Mergesort (divide-and-conquer)
Divide array into two halves.
A L G O R I T H M S
A L G O R I T H M S divide
Merge Sort
•Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
Mergesort
•Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
Merge two halves to make sorted whole.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
A G H I L M O R S T merge
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O R auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S T auxiliary array
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers
cn/2 cn/2 cn
lg n
c c c c c c cn
Total : cnlgn+cn
Example
3, 8, 1, 5, 9, 2, 0, 7, 4, 1, 7
Sorting Algorithms so far
• Insertion sort
Worst-case running time O(n2)
• Merge sort
Worst-case running time O(n log n), but requires
additional memory O(n)