Skip to content

Instantly share code, notes, and snippets.

@iamamit-107
Created May 9, 2020 16:47
Show Gist options
  • Save iamamit-107/d877e97faccd1c7477c9ae889c09016f to your computer and use it in GitHub Desktop.
Save iamamit-107/d877e97faccd1c7477c9ae889c09016f to your computer and use it in GitHub Desktop.
function insertionSort(arr) {
const len = arr.length;
for (let i = 0; i < len; i++) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
return arr;
}
console.log(insertionSort([20, 5, 15, 35, 10]));
//output: [ 5, 10, 15, 20, 35 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment