Merge Sort
Last updated
Was this helpful?
Last updated
Was this helpful?
As a typical DnC algorithm, MergeSort tries to solve sorting problem in a recursive, distributed way. Its overall algorithm outperforms the and canonical algorithms.
MergeSort algorithm takes the unsorted original array and split it into С number of sub-arrays, feeding them individually into the algorithm itself for the same sorting purpose.
Then, resolve each sub-problem with the defined minimal length of sub-array. After recursive calls in each layer of division, combining the results of each subroutine to generate the final sorting solution.
Note: the task's aim might varies with scenarios. e.g. a bulk of sorted arrays reside in memory has to merge into an array with randomly filled elements; therefore, the actual implementation is not definite.
Though the abstract details are the same, the and are two different implementations of . The first one is suitable for education for its simplicity while the second one has a slightly better performance and widely adopted in many libraries.
It is a common practice to use such a recursive approach:
rather than a recursive method in above approach, using a iterative method:
and the universal MERGE step:
is a stable sorting scheme that has running times of Ο(n ⋅ log(n)) regardless of the inputs.
It is simple to prove the running complexity by Τ(n) = 2 Τ(n/2) + Θ(n)
Combining and :