Last active
October 27, 2017 02:26
-
-
Save AaronFlower/5b60e5f841fccddee1e2e4c62adb8127 to your computer and use it in GitHub Desktop.
Insertion sort javascript version
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(data) | |
{ | |
for(let i = 1; i < data.length; ++i) { | |
let j = i | |
while(j > 0 && data[j] < data[j - 1]) { | |
let tmp = data[j] | |
data[j] = data[j - 1] | |
data[j - 1] = tmp | |
j -- | |
} | |
} | |
} | |
data = Array.from({length: 20}).map(() => Math.floor(Math.random() * 20)) | |
console.log(data) | |
insertionSort(data) | |
console.log(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a better insertion introduction with generator.