Skip to content

Insertion Sort

insertionSort(arr)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
const insertionSort = arr => {
  for (let i = 1; i < arr.length; i++) {
    let curr = arr[i];

    for (var j = i - 1; j >= 0 && arr[j] > curr; j--)
      arr[j + 1] = arr[j];

    arr[j + 1] = curr;
  }

  return arr;
};