Mergesort New
Mergesort New
Divide-and-Conquer
• If we can break a single big problem into smaller sub-problems, solve the smaller sub-problems and
combine their solutions to find the solution for the original big problem, it becomes easier to solve the
whole problem.
2
How Merge Sort Works?
• In Merge Sort, the given unsorted array with n elements, is divided into n subarrays, each
having one element, because a single element is always sorted in itself. Then, it repeatedly
merges these subarrays, to produce new sorted subarrays, and in the end, one complete
sorted array is produced.
• In merge sort, we break the given array midway, for example if the original array had 6
elements, then merge sort will break it down into two subarrays with 3 elements each.
• But breaking the original array into 2 smaller subarrays is not helping us in sorting the array.
• So we will break these subarrays into even smaller subarrays, until we have multiple
subarrays with single element in them. Now, the idea here is that an array with a single
element is already sorted, so once we break the original array into subarrays which has only
a single element, we have successfully broken down our problem into base problems.
• And then we have to merge all these sorted subarrays, step by step to form one single
sorted array.
• In merge sort we follow the following steps:
1. We take a variable p and store the starting index of our array in this. And we
take another variable r and store the last index of array in it.
2. Then we find the middle of the array using the formula (p + r)/2 and mark
the middle index as q, and break the array into two subarrays, from p to q
and from q + 1 to r index.
3. Then we divide these 2 subarrays again, just like we divided our main array
and this continues.
4. Once we have divided the main array into subarrays with single elements,
then we start merging the subarrays.
• Let's consider
an array with
values {14, 7,
3, 12, 9, 11, 6,
12}
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
6
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
• Every recursive algorithm is dependent on a base case and the ability to combine the
results from base cases. Merge sort is no different. The most important part of the
merge sort algorithm is, you guessed it, merge step.
• The merge step is the solution to the simple problem of merging two sorted
lists(arrays) to build one large sorted list(array).
• Every recursive algorithm is dependent on a base case and the ability to combine the
results from base cases. Merge sort is no different. The most important part of the
merge sort algorithm is, you guessed it, merge step.
• The merge step is the solution to the simple problem of merging two sorted
lists(arrays) to build one large sorted list(array).
• The algorithm maintains three pointers, one for each of the two arrays
and one for maintaining the current index of the final sorted array.