logo
SEE ALGORITHMS
    Bubble Sort
    Insertion Sort
    Selection Sort
    Radix Sort
    Heap Sort
    Merge Sort
    Quick Sort

Merge Sort

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.

Things to Observe

  • Divide Recursively: Watch how the algorithm first breaks the array down recursively into single-element sub-arrays. The visualization shows this by moving the elements downwards.
  • Conquer (Merge): Observe how these sub-arrays are then merged back together in sorted order. This merging step is where the core sorting logic happens, comparing elements from the sub-arrays and placing them into a temporary array before updating the main one.
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)
Select number of elements:  

© 2025 SEE Algorithms. Code licensed under MIT, content under CC BY-NC 4.0.