1. Sort function
Define the recursive function that keeps splitting and merging the array.
function mergeSort(arr)Typorithm · medium
Step 1 of 37 · intro card
Sorting Algorithms
Merge Sort divides the array, solves smaller halves, then merges them back in order.
Merge Sort uses divide and conquer. It keeps splitting the array until each piece has one value.
The base case is `arr.length <= 1`: a one-item array is already sorted.
After splitting with `Math.floor(arr.length / 2)`, the left and right halves are recursively sorted.
The merge helper combines sorted halves by repeatedly taking the smaller front value.
Using `left[i] <= right[j]` keeps equal left-side values first, so this implementation is stable.
Divide until subarrays have one element.
Recursively sort left and right halves.
Merge by comparing front values.
Time is O(n log n) in best, average, and worst cases.
Space is O(n), and this implementation is not in-place.
function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
function merge(left, right) {
const result = [];
let i = 0;
let j = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
result.push(left[i]);
i++;
} else {
result.push(right[j]);
j++;
}
}
return result.concat(left.slice(i), right.slice(j));
}Connect each part of the code to the concept before you type.
Define the recursive function that keeps splitting and merging the array.
function mergeSort(arr)A length 0 or 1 array is already sorted, so recursion stops here.
if (arr.length <= 1) return arrFind the integer split point for dividing the array into two halves.
const mid = Math.floor(arr.length / 2)Recursively sort the left half before merging it back.
const left = mergeSort(arr.slice(0, mid))Recursively sort the right half before merging it back.
const right = mergeSort(arr.slice(mid))Combine the two sorted halves into one sorted array.
return merge(left, right)This helper repeatedly picks the smaller front value from two sorted arrays.
function merge(left, right)Store the merged output in a new array, which is why this version uses extra space.
const result = []Track the current front value in the left sorted half.
let i = 0Track the current front value in the right sorted half.
let j = 0Compare front values while both halves still have values available.
while (i < left.length && j < right.length)Use <= so equal values from the left half keep their relative order.
if (left[i] <= right[j])When the left front value is smaller or equal, add it to the result.
result.push(left[i])Move to the next value in the left half after taking left[i].
i++When the right front value is smaller, add it to the result.
result.push(right[j])Move to the next value in the right half after taking right[j].
j++Append whatever remains after one half runs out.
return result.concat(left.slice(i), right.slice(j))Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
Input[5,1,4,2,8]
Copied halves and a merge buffer, plus O(log n) recursive frames. n is the input length. The input array is not counted as extra space.
One reusable value slot. Empty until the next operation needs it.
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state