Runtime: O(nlogn) Auxiliary Space : O(n)
Merge Sort divides the array in half, sorts each of those halves and then merges them back together. Each of these halves has the same sorting algorithm applied to it. Eventually, its like merging two single-element arrays. The merge method operates by copying all the elements from target array segment into a helper array,keeping track of where the start of the left and right halves should be. Then, iterate through helper, copying the smaller element from each half into the array.
It is a stable algorithm.
Implementation of MergeSort
|
|
Drawbacks
- It is slower compared to other sort algorithms for smaller tasks.
- It requires additional memory space of O(n).
- Merge sort goes through whole process even if array is sorted.