Skip to main content
Merge Sort
Merge Sort
https://www.cs.uiowa.edu/~hzhang/c31/notes/
https://www.cs.uiowa.edu/~hzhang/c31/notes/mergesort
mergesort.
.ppt
ppt
Merging
Merging
The key to Merge Sort is merging two sorted
lists into one, such that if you have two lists
X (x1x2…
xm) and Y(y1y2…
yn) the
resulting list is Z(z1z2…
zm+n)
Example:
L1 = { 3 8 9 } L2 = { 1 5 7 }
merge(L1, L2) = { 1 3 5 7 8 9 }
Merging
Merging (cont.)
(cont.)
3 10 23 54 1 5 25 75
X: Y:
Result:
Merging
Merging (cont.)
(cont.)
3 10 23 54 5 25 75
1
X: Y:
Result:
Merging
Merging (cont.)
(cont.)
10 23 54 5 25 75
1 3
X: Y:
Result:
Merging
Merging (cont.)
(cont.)
10 23 54 25 75
1 3 5
X: Y:
Result:
Merging
Merging (cont.)
(cont.)
23 54 25 75
1 3 5 10
X: Y:
Result:
Merging
Merging (cont.)
(cont.)
54 25 75
1 3 5 10 23
X: Y:
Result:
Merging
Merging (cont.)
(cont.)
54 75
1 3 5 10 23 25
X: Y:
Result:
Merging
Merging (cont.)
(cont.)
75
1 3 5 10 23 25 54
X: Y:
Result:
Merging
Merging (cont.)
(cont.)
1 3 5 10 23 25 54 75
X: Y:
Result:
Divide And Conquer
Divide And Conquer
 Merging a two lists of one element each is the
same as sorting them.
 Merge sort divides up an unsorted list until the
above condition is met and then sorts the divided
parts back together in pairs.
 Specifically this can be done by recursively
dividing the unsorted list in half, merge sorting the
right side then the left side and then merging the
right and left back together.
Merge Sort Algorithm
Merge Sort Algorithm
Given a list L with a length k:
If k == 1  the list is sorted
Else:
– Merge Sort the left side (1 thru k/2)
– Merge Sort the right side (k/2+1 thru k)
– Merge the right side with the left side
Merge Sort Example
Merge Sort Example
99 6 86 15 58 35 86 4 0
Merge Sort Example
Merge Sort Example
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Merge Sort Example
Merge Sort Example
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 15
99 6 58 35 86 4 0
Merge Sort Example
Merge Sort Example
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 15
99 6 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Merge Sort Example
Merge Sort Example
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 15
99 6 58 35 86 4 0
99 6 86 15 58 35 86 4 0
4 0
Merge Sort Example
Merge Sort Example
99 6 86 15 58 35 86 0 4
4 0
Merge
Merge Sort Example
Merge Sort Example
15 86
6 99 58 35 0 4 86
99 6 86 15 58 35 86 0 4
Merge
Merge Sort Example
Merge Sort Example
6 15 86 99 0 4 35 58 86
15 86
6 99 58 35 0 4 86
Merge
Merge Sort Example
Merge Sort Example
0 4 6 15 35 58 86 86 99
6 15 86 99 0 4 35 58 86
Merge
Merge Sort Example
Merge Sort Example
0 4 6 15 35 58 86 86 99
Implementing Merge Sort
Implementing Merge Sort
 There are two basic ways to implement merge sort:
– In Place: Merging is done with only the input array
 Pro: Requires only the space needed to hold the array
 Con: Takes longer to merge because if the next element is in
the right side then all of the elements must be moved down.
– Double Storage: Merging is done with a temporary array of the
same size as the input array.
 Pro: Faster than In Place since the temp array holds the
resulting array until both left and right sides are merged into
the temp array, then the temp array is copied over the input
array.
 Con: The memory requirement is doubled.
mergeSort(arr, int left, int right)
mergeSort(arr, int left, int right)
{
{
if (left >= right)
if (left >= right)
return;
return;
int mid = (left + right) / 2;
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
merge(arr, left, mid, right);
}
}
Merge(arr, beg, mid, end)
{
int start1=beg, start2=mid+1,pos=0;
while(start1<=mid && start2<=end)
{
if(arr[start1]<=arr[start2])
{arr_temp[pos++]=arr[start1]; start1++;}
else
{arr_temp[pos++]=arr[start2]; start2++;}
}
if (start1>mid)
while(start2<=end)
{arr_temp[pos++]=arr[start2++];}
if(start2>end)
while(start1<=mid)
{arr_temp[pos++]=arr[start1++];}
copy arr_temp to arr(beg, end)
}
HW: Check if logic works
Merge Sort Analysis
Merge Sort Analysis
The Double Memory Merge Sort runs O (N log N) for
all cases, because of its Divide and Conquer approach.
T(N) = 2T(N/2) + N = O(N logN)
Merge sort
Merge sort
Recurrence equation:
Recurrence equation:
c
c1
1 if n=1
if n=1
T(n) = 2T(n/2) + c.n if n>1
T(n) = 2T(n/2) + c.n if n>1
MergeSort(arr[], l, r) If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
L1.28
Recursion tree
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
(1)
…
h = lg n
cn
cn
cn
#leaves = n (n)
Total(n lg n)
…
Solution
Solution
By Substitution:
T(n) = 2T(n/2) + c2n
T(n/2) = 2T(n/4) + c2n/2
T(n) = 4T(n/4) + 2 c2n
T(n) = 8T(n/8) + 3 c2n
T(n) = 2i
T(n/2i
) + ic2n
Assuming n = 2k
, expansion halts when we get T(1) on right side; this
happens when i=k T(n) = 2k
T(1) + kc2n
Since 2k
=n, we know k=logn; since T(1) = c1, we get
T(n) = c1n + c2nlogn;
thus an upper bound for TmergeSort(n) is O(nlogn)
Assume that a merge sort algorithm in the worst case takes
30 seconds for an input of size 64. Which of the following
most closely approximates the maximum input size of a
problem that can be solved in 6 minutes? (GATE 2015)
1. 256
2. 512
3. 1024
4. 2048

Finally…
Finally…
There are other variants of Merge Sorts including k-
way merge sorting, but the common variant is the Double
Memory Merge Sort. Though the running time is O(N logN)
and runs much faster than insertion sort and bubble sort,
merge sort’s large memory demands makes it not very
practical for main memory sorting.
Questions
Questions

H.W
 If we partition into three(or k) parts instead of two, what would be the
complexity of the algorithm ? What if we do a n-way merge sort ?
 Would you prefer to use a three part merge sort –why or why not ?
 What about a ternary search instead of binary search
 Would you prefer 2-way merge sort to merge sort. Why or why not
33