0% found this document useful (0 votes)
44 views11 pages

Mergesort New

Merge sort uses a divide and conquer approach to sort arrays. It divides the array into halves, recursively sorts the halves, and then merges the sorted halves back together into a fully sorted array. It breaks down the problem into sorting individual elements, which can then be merged back together. The key step is the merge, which uses three pointers to efficiently merge two sorted subarrays into a single sorted array or list.

Uploaded by

Bhupesh Dhapola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views11 pages

Mergesort New

Merge sort uses a divide and conquer approach to sort arrays. It divides the array into halves, recursively sorts the halves, and then merges the sorted halves back together into a fully sorted array. It breaks down the problem into sorting individual elements, which can then be merged back together. The key step is the merge, which uses three pointers to efficiently merge two sorted subarrays into a single sorted array or list.

Uploaded by

Bhupesh Dhapola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Merge Sort

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.

• Divide the problem into a number of sub-problems


• Similar sub-problems of smaller size

• Conquer the sub-problems


• Solve the sub-problems recursively

• Sub-problem size small enough  solve the problems in straightforward manner

• Combine the solutions of the sub-problems


• Obtain the solution for the original 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

if p < r Check for base case

then q ← (p + r)/2 Divide

MERGE-SORT(A, p, q) Conquer

MERGE-SORT(A, q + 1, r) Conquer

MERGE(A, p, q, r) Combine

• Initial call: MERGE-SORT(A, 1, n)


7
The merge Step of Merge Sort

• 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.

Have we reached the end of any of the arrays?


 No:
• Compare current elements of both arrays
• Copy smaller element into sorted array
• Move pointer of element containing smaller element
 Yes:
• Copy all remaining elements of non-empty array
• Our task is to merge two subarrays A[p..q] and A[q+1..r] to create a sorted
array A[p..r]. So the inputs to the function are A, p, q and r
• The merge function works as follows:
1. Create copies of the subarrays L ← A[p..q] and M ← A[q+1..r].
2. Create three pointers i, j and k
• i maintains current index of L, starting at 1
• j maintains current index of M, starting at 1
• k maintains the current index of A[p..q], starting at p.
3. Until we reach the end of either L or M, pick the larger among the
elements from L and M and place them in the correct position at A[p..q]
4. When we run out of elements in either L or M, pick up the remaining
elements and put in A[p..q]

You might also like