Created
March 8, 2019 11:16
-
-
Save dlucidone/ec567bd9662dc54c08847b39145f14fb to your computer and use it in GitHub Desktop.
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
const insertionSort = a => { | |
var n = a.length; | |
for(var i=1;i<n;i++){ | |
var key = a[i]; | |
var j = i-1; | |
while(j>=0 && a[j]>key){ | |
a[j+1] = a[j]; | |
j=j-1; | |
} | |
a[j+1] = key; | |
} | |
console.log(a); | |
}; | |
insertionSort([5,3,1,2,6,4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment