Back to Dashboard
Merge Sort
A divide-and-conquer algorithm that consistently divides the array into halves, sorts them, and merges them back together.
FastSlow
Initializing...
Time Complexity
Best
O(n log n)
Average
O(n log n)
Worst
O(n log n)
Space Complexity
O(n)
StabilityTRUE
In-placeFALSE
Explanation
Merge Sort is an efficient, comparison-based, divide-and-conquer sorting algorithm. It works by recursively dividing the array into halves until each sub-array contains only one element, then merging those sub-arrays back together in sorted order. It has a consistent time complexity of O(n log n), making it very reliable for large datasets, though it requires O(n) extra space for the merging process.
Pseudo Code
1function mergeSort(array)2 if array.length <= 1 return array34 mid = floor(array.length / 2)5 left = mergeSort(array[0...mid])6 right = mergeSort(array[mid...end])78 return merge(left, right)910function merge(left, right)11 result = []12 while left and right have elements13 if left[0] <= right[0]14 append left[0] to result15 else16 append right[0] to result17 return result + remaining
Sponsor Ad