Insertion Sort

Sort an array using the insertion sort algorithm by building a sorted portion one element at a time.

Code

Algorithms
const a = [...arr];
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;

Parameters

Array to sort

Browser·fetch() may be limited by CORS

More JavaScript Snippets