Skip to content

Instantly share code, notes, and snippets.

@eengineergz
Created February 27, 2021 05:11
Show Gist options
  • Save eengineergz/a9f4b8596c7546ac92746db659186d8c to your computer and use it in GitHub Desktop.
Save eengineergz/a9f4b8596c7546ac92746db659186d8c to your computer and use it in GitHub Desktop.
function insertionSort(array) {
for (let i = 1; i < array.length; i++) {
let value = list[i];
let hole = i;
while (hole > 0 && list[hole - 1] > value) {
list[hole] = list[hole - 1];
hole--;
}
list[hole] = value;
}
return array;
}
//Alt Solution--------------------------------------------
function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
let current = arr[i];
let j = i - 1;
while (j > -1 && current < arr[j]) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = current;
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment