Created
May 9, 2020 16:47
-
-
Save iamamit-107/d877e97faccd1c7477c9ae889c09016f to your computer and use it in GitHub Desktop.
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
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