Created
January 11, 2019 20:43
-
-
Save alexcambose/a5ce5556eb58b538900d1f606ad1d6ee to your computer and use it in GitHub Desktop.
JavaScript Insertion Sort
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
let array = [3,1,4,5,3,2,6]; | |
for(let i = 1; i < array.length; i++) { | |
let j = i - 1; // array[0...i-1] is sorted | |
let temp = array[i]; | |
while(j >= 0 && array[j] > temp) { | |
array[j+1] = array[j]; | |
j--; | |
} | |
array[j+1] = temp; | |
} | |
console.log(array); // [1, 2, 3, 3, 4, 5, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment