Skip to content

Instantly share code, notes, and snippets.

@dansuh17
Created February 15, 2018 09:44
Show Gist options
  • Save dansuh17/37e5dae465d50f60680832b06487aae9 to your computer and use it in GitHub Desktop.
Save dansuh17/37e5dae465d50f60680832b06487aae9 to your computer and use it in GitHub Desktop.
Generate n-permutations
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