Concepts

Merge sort

Try the free JavaScript compiler

Divide into balanced halves; recursively sort and linearly merge. With left-first ties, stable; O(n log n) time under standard comparison model.

Formal Syntax & Structure

javascript syntaxjavascript
merge(leftSorted, rightSorted); mergeSort(values)

Working Example

Example Snippetjavascript
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])));
JavaScript Reference