Stable insertion sorting and numeric comparisons
Quick answer
Insertion sort grows a sorted prefix by shifting larger items to the right and inserting the next item. It is simple for small inputs and nearly sorted data.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
function insertion(values) { const a=values.slice(); for(let i=1;i<a.length;i++){ const key=a[i];let j=i-1; while(j>=0 && a[j]>key){a[j+1]=a[j];j--;} a[j+1]=key; } return a;}const original=[3,1,2,1];console.log(JSON.stringify(insertion(original)),JSON.stringify(original));Expected console output:
[1,1,2,3] [3,1,2,1]The copied array is sorted; the original is unchanged.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const rows=[{id:"A",p:2},{id:"B",p:1},{id:"C",p:2}];const ordered=rows.toSorted((a,b)=>a.p-b.p);console.log(ordered.map(x=>x.id).join(""),rows.map(x=>x.id).join(""));Expected console output:
BAC ABCThe equal-priority A and C preserve their relative order.
How it works and cost model
Copy the input if mutation is not allowed. At iteration i, items before i are sorted. Save the key, move each strictly larger predecessor one step right, and place the key in the resulting gap. Strict comparison preserves the order of equal keys (stability). Worst-case moves and comparisons are O(n²); already sorted data requires only linear comparisons. The in-place version uses O(1) extra scalar storage; copying costs O(n). Built-in Array.sort mutates its receiver, defaults to string ordering, and is stable in current ECMAScript; its time and space implementation are not specified. toSorted returns a new array.
Common mistakes
Do not shift equal keys with >= if stability matters. For numeric built-in sorting, supply (a,b) => a - b for finite numbers.
Practice
Implement insertion sort for records by priority. Confirm equal priorities retain original order and the input array remains unchanged.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Stable insertion sorting and numeric comparisons — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
What invariant holds before each insertion-sort iteration i?
Which shift condition preserves insertion-sort stability for equal numeric keys?
Which claim about built-in JavaScript sort complexity is supported by the language contract?
What is the exact console output in the Stable insertion sort example 1?
function insertion(values) { const a=values.slice(); for(let i=1;i<a.length;i++){ const key=a[i];let j=i-1; while(j>=0 && a[j]>key){a[j+1]=a[j];j--;} a[j+1]=key; } return a;}const original=[3,1,2,1];console.log(JSON.stringify(insertion(original)),JSON.stringify(original));What is the exact console output in the Stable insertion sort example 2?
const rows=[{id:"A",p:2},{id:"B",p:1},{id:"C",p:2}];const ordered=rows.toSorted((a,b)=>a.p-b.p);console.log(ordered.map(x=>x.id).join(""),rows.map(x=>x.id).join(""));