Merging
Merging
The key toMerge Sort is merging two sorted
lists into one, such that if you have two lists
X (x1x2…
xm) and Y(y1y2…
yn) the
resulting list is Z(z1z2…
zm+n)
Example:
L1 = { 3 8 9 } L2 = { 1 5 7 }
merge(L1, L2) = { 1 3 5 7 8 9 }
Divide And Conquer
DivideAnd 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.
13.
Merge Sort Algorithm
MergeSort 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
Implementing Merge Sort
ImplementingMerge 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.
25.
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
26.
Merge Sort Analysis
MergeSort 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)
27.
Merge sort
Merge sort
Recurrenceequation:
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)
28.
L1.28
Recursion tree
Recursion tree
SolveT(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)
…
29.
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)
30.
Assume that amerge 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
32.
Finally…
Finally…
There are othervariants 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.
33.
Questions
Questions
H.W
If wepartition 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