Last active
November 26, 2019 23:28
-
-
Save mitrakmt/9e1b3440db54daa78bd9a4db0ceea3c1 to your computer and use it in GitHub Desktop.
Exploring insertion sort in JavaScript.
This file contains 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
const insertionSort = (array) => { | |
const length = array.length; | |
for (let i = 1; i < length; i++) { | |
let key = array[i]; | |
let j = i - 1; | |
while (j >= 0 && array[j] > key) { | |
array[j + 1] = array[j]; | |
j = j - 1; | |
} | |
array[j+1] = key; | |
} | |
return array; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment