Skip to content

Instantly share code, notes, and snippets.

@billykwok
Created January 16, 2016 23:42
Show Gist options
  • Save billykwok/5e314a48a9ef9ae13b54 to your computer and use it in GitHub Desktop.
Save billykwok/5e314a48a9ef9ae13b54 to your computer and use it in GitHub Desktop.
Recursive Version of Insertion Sort in ES6
function insertSortRec(array) {
if (array.length <= 1) {
return array;
} else {
const key = array.pop();
let subArray = insertSortRec(array);
let i = subArray.length - 1;
while (i >= 0 && subArray[i] > key) {
subArray[i + 1] = subArray[i];
--i;
}
subArray[i + 1] = key;
return subArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment