Skip to content

Instantly share code, notes, and snippets.

@andresabello
Created June 7, 2020 00:11
Show Gist options
  • Save andresabello/410b39e59bb9aa81e2ac411943d499fe to your computer and use it in GitHub Desktop.
Save andresabello/410b39e59bb9aa81e2ac411943d499fe to your computer and use it in GitHub Desktop.
function insertionSort(array) {
const length = array.length
for (i = 0; i < length; i++) {
if (array[i] > array[i + 1]) {
let temp = array[i]
array[i] = array[i + 1]
array[i + 1] = temp
for (j = i - 1; j >= 0; j--) {
if (array[j] > array[j + 1]) {
let temp = array[j]
array[j] = array[j + 1]
array[j + 1] = temp
}
}
}
}
return array
}
insertionSort([99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment