Created
January 16, 2016 23:42
-
-
Save billykwok/5e314a48a9ef9ae13b54 to your computer and use it in GitHub Desktop.
Recursive Version of Insertion Sort in ES6
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
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