Merge Sort

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 array
3
4 mid = floor(array.length / 2)
5 left = mergeSort(array[0...mid])
6 right = mergeSort(array[mid...end])
7
8 return merge(left, right)
9
10function merge(left, right)
11 result = []
12 while left and right have elements
13 if left[0] <= right[0]
14 append left[0] to result
15 else
16 append right[0] to result
17 return result + remaining
Sponsor Ad