All examples

Insertion sort, .

Builds a sorted prefix by shifting larger elements right.

insertion-sort.js
function insertionSort(a) {
  for (let i = 1; i < a.length; i++) {
    let key = a[i];
    let j = i - 1;
    while (j >= 0 && a[j] > key) {
      a[j + 1] = a[j];
      j = j - 1;
    }
    a[j + 1] = key;
  }
  return a;
}

insertionSort([8, 3, 7, 1, 4]);

What to watch

Values shift one slot right to open a gap, then key drops into place - the array animates the shift.

Reading insertion sorton a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how insertion sort works.

Run insertion sort yourself

Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.

Open NeonFlow