Created
February 15, 2018 09:44
-
-
Save dansuh17/37e5dae465d50f60680832b06487aae9 to your computer and use it in GitHub Desktop.
Generate n-permutations
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
var allPerms = []; | |
function permutation(list, ret) | |
{ | |
if (list.length == 0) { | |
allPerms.push(ret); // return the result | |
return; | |
} | |
for (var i = 0; i < list.length; i++) { | |
var x = list.splice(i, 1); // delete the i_th element | |
ret.push(x); | |
perm(list, ret); // recursively call with one less element | |
ret.pop(); | |
list.splice(i, 0, x); // put back the element | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment