Mastery Points
0

Merge Sort in dsa

Context & Logic

Merge Sort splits the array recursively and then merges halves.

Example

function mergeSort(arr) {
    if (arr.length <= 1) return arr;
    const mid = Math.floor(arr.length / 2);
    const left = mergeSort(arr.slice(0, mid)), right = mergeSort(arr.slice(mid));
    return merge(left, right);
}

Step-by-Step Logic

1

Divide the array into two halves recursively.

2

Merge the two sorted halves.

Complexity Metrics

Time Efficiency

O(n log n)

Memory Footprint

O(n)