Skip to content

Instantly share code, notes, and snippets.

@imsarvesh
Created January 10, 2019 16:47
Show Gist options
  • Save imsarvesh/7ce62791fe18e28c49429524082ce27a to your computer and use it in GitHub Desktop.
Save imsarvesh/7ce62791fe18e28c49429524082ce27a to your computer and use it in GitHub Desktop.
var insertionSort = (arr) => {
arr = arr.slice();
for(var i = 1; i < arr.length; i++){
var current = arr[i];
var j = i - 1;
while(j >= 0 && arr[j] > current){
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = current;
}
return arr;
}
insertionSort([5,4,3,2,1]) //[1,2,3,4,5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment