Merge Sort
Speed
About algorithm
Explanation
Merge Sort is a divide-and-conquer algorithm that recursively splits the array into halves, sorts each half, and then merges the sorted halves to produce a sorted array.
Steps
- Divide the array into two halves.
- Recursively sort each half.
- Merge the two sorted halves into one sorted array.
- Repeat until the entire array is sorted.
Pseudocode
mergeSort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) / 2
left = mergeSort(arr[:mid])
right = mergeSort(arr[mid:])
return merge(left, right)Complexity
Time: O(n log(n))
Space: O(n)