Merge sort and divide-and-conquer
Quick answer
Merge sort divides a sequence into halves, sorts the halves, and merges them in order. The merge step compares only the next unused elements of two sorted runs.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
function merge(a,b){ let i=0,j=0;const out=[]; while(i<a.length && j<b.length) out.push(a[i]<=b[j]?a[i++]:b[j++]); return out.concat(a.slice(i),b.slice(j));}console.log(JSON.stringify(merge([1,4,7],[2,4,8])));Expected console output:
[1,2,4,4,7,8]Each comparison selects the next smallest remaining item.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
function sort(a){ if(a.length<2)return a.slice(); const m=Math.floor(a.length/2),l=sort(a.slice(0,m)),r=sort(a.slice(m)); let i=0,j=0;const out=[]; while(i<l.length&&j<r.length)out.push(l[i]<=r[j]?l[i++]:r[j++]); return out.concat(l.slice(i),r.slice(j));}console.log(JSON.stringify(sort([5,-1,3,0])),JSON.stringify(sort([])));Expected console output:
[-1,0,3,5] []Sorted halves merge into ascending order; empty input is the base case.
How it works and cost model
Use two indices for merging, advancing the side that supplies the smaller item; when one side ends, append the other remainder. Choosing the left item on equal keys preserves stability. With balanced splitting and linear merging, the model gives Theta(n log n) time for n >= 2; at each level the total items merged is n. This educational implementation returns new arrays and uses O(n) peak live array storage plus O(log n) recursion frames in the usual allocation model. It allocates across multiple levels, so total allocation work is not the same as peak live space.
Common mistakes
Do not repeatedly shift() from the front of arrays and assume constant-time extraction. Merge only runs already sorted with the same comparator.
Practice
Merge [1,4,7] and [2,4,8] with two indices. Then run merge sort on an empty array, a singleton and a reversed array.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Merge sort and divide-and-conquer — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
What is required before the two-pointer merge step?
Why does balanced merge sort have a logarithmic number of levels?
What tie-breaking choice preserves stability during a merge?
What is the exact console output in the Merge sort example 1?
function merge(a,b){ let i=0,j=0;const out=[]; while(i<a.length && j<b.length) out.push(a[i]<=b[j]?a[i++]:b[j++]); return out.concat(a.slice(i),b.slice(j));}console.log(JSON.stringify(merge([1,4,7],[2,4,8])));What is the exact console output in the Merge sort example 2?
function sort(a){ if(a.length<2)return a.slice(); const m=Math.floor(a.length/2),l=sort(a.slice(0,m)),r=sort(a.slice(m)); let i=0,j=0;const out=[]; while(i<l.length&&j<r.length)out.push(l[i]<=r[j]?l[i++]:r[j++]); return out.concat(l.slice(i),r.slice(j));}console.log(JSON.stringify(sort([5,-1,3,0])),JSON.stringify(sort([])));