Merge
Merge
Definition
• Like Quick Sort, Merge Sort is a Divide and
Conquer algorithm.
• It divides input array in two halves, calls itself for
the two halves and then merges the two sorted
halves.
• The merge() function is used for merging two
halves.
Steps
• Find the middle index of the array to divide it in two
halves: m = (l+r)/2.
• Call MergeSort for first half: mergeSort(array, l, m)
• Call mergeSort for second half: mergeSort(array, m+1,
r)
• Recursively, merge the two halves in a sorted manner,
so that only one sorted array is left:
Example
• The following diagram shows the complete merge sort
process for an example array {38, 27, 43, 3, 9, 82, 10}. If we
take a closer look at the diagram, we can see that the array
is recursively divided in two halves till the size becomes 1.
Once the size becomes 1, the merge processes comes into
action and starts merging arrays back till the complete array
is merged.
Made By :- AKTU WALA ( Satyam Sahu )