Created
November 20, 2021 16:34
-
-
Save claytonjwong/1176f11758cf157caa54c9b1f2d750b2 to your computer and use it in GitHub Desktop.
Returns the permutations of the array A
This file contains hidden or 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
let permutations = A => { | |
let N = A.length; | |
let perms = []; | |
let go = (i = 0) => { | |
if (i == N) { | |
perms.push([...A]); | |
return; | |
} | |
for (let k = i; k < N; ++k) { | |
[A[i], A[k]] = [A[k], A[i]]; | |
go(i + 1); | |
[A[i], A[k]] = [A[k], A[i]]; | |
} | |
}; | |
go(); | |
return perms; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment