Merge Sort is more advanced, divide-and-conquer algorithm that recursively splits an unsorted list into smaller sublists until each contains a single element. These sublists are then merged back together in a sorted manner. With a time complexity of O(n log n), Merge Sort is efficient and stable, making it suitable for handling large datasets.
function merge(start, mid, end):
i = start, j = mid + 1
temp = []
while i <= mid and j <= end:
if arr[i] <= arr[j]:
append arr[i] to temp
i = i + 1
else:
append arr[j] to temp
j = j + 1
while i <= mid:
append arr[i] to temp
i = i + 1
while j <= end:
append arr[j] to temp
j = j + 1
for i = start to end:
arr[i] = temp[i - start]
function mergeSort(start, end):
if start < end:
mid = (start + end) / 2
mergeSort(start, mid)
mergeSort(mid + 1, end)
merge(start, mid, end)
© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0.