function permuteReganKoopmans(list) {
if (list.length == 1) { return [list] }
let permutations = []
let subpermutations = permuteReganKoopmans(list.slice(1, list.length))
for (index in subpermutations) {
let sublist = subpermutations[index]
for (let pos = 0; pos < sublist.length+1; pos++) {
permutations.push(sublist.slice(0, pos)
.concat([list[0]])
.concat(sublist.slice(pos, sublist.length)));
}
}
return permutations;
}
const permutePrzemek = arr => {
const permutations = [];
const swap = (a, b) => {
const tmp = arr[a];
arr[a] = arr[b];
arr[b] = tmp;
};
const generate = n => {
if (n === 1) {
return permutations[permutations.length] = arr;
}
for (let i = 0; i < n; i++) {
generate(n - 1);
swap(n % 2 === 0 ? i : 0, n - 1);
}
};
generate(arr.length);
return permutations;
}