Skip to content

Instantly share code, notes, and snippets.

@dlucidone
Created March 8, 2019 11:16
Show Gist options
  • Save dlucidone/ec567bd9662dc54c08847b39145f14fb to your computer and use it in GitHub Desktop.
Save dlucidone/ec567bd9662dc54c08847b39145f14fb to your computer and use it in GitHub Desktop.
Insertion Sort
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