Created
June 7, 2020 00:11
-
-
Save andresabello/410b39e59bb9aa81e2ac411943d499fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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