function swap(index1, index2) {
tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
function generate(int) {
if (int === 1) {
// Make sure to join the characters as we create the permutation arrays
order.push(arr.join(''));
} else {
for (var i = 0; i != int; ++i) {
generate(int - 1);
swap(int % 2 ? 0 : i, int - 1);
}
}
}
var order = [];
arr=['a', 'b', 'c', 'd'];
generate(arr.length);
console.log(order);
Heap's Algorithm