Insertion Sort:: singly linked list

function insertionSort(arr) {
  for (let i = 0; i < arr.length; i++) {
    const curr = arr[i];
    for (let j = i - 1; j >= 0; j--) {
      if (arr[j] <= curr) break;
      swap(arr, j, j + 1);
    }
  }

  return arr;
}

function swap(arr, a, b) {
  const temp = arr[a];
  arr[a] = arr[b];
  arr[b] = temp;
}

// Do not edit the line below.
exports.insertionSort = insertionSort;

Last updated