Permutation of Array

Permutation of array

If you're using C++, you can use std::next_permutation from the <algorithm> header file:

int a[] = {3,4,6,2,1};
int size = sizeof(a)/sizeof(a[0]);
std::sort(a, a+size);
do {
// print a's elements
} while(std::next_permutation(a, a+size));

Finding all permutations of array elements as concatenated strings

Heap's algorithm can be used to generate all permutations (without repetitions) of array elements, you can then use those permutations with array.join("") to convert them to strings. This works for arrays of any size and any type:

Here is a quick javascript implementation:

// some global variable to store the results
var result = []

// currentSize should be invoked with the array size
function permutation(arr, currentSize) {
if (currentSize == 1) { // recursion base-case (end)
result.push(arr.join(""));
return;
}

for (let i = 0; i < currentSize; i++){
permutation(arr, currentSize - 1);
if (currentSize % 2 == 1) {
let temp = arr[0];
arr[0] = arr[currentSize - 1];
arr[currentSize - 1] = temp;
} else {
let temp = arr[i];
arr[i] = arr[currentSize - 1];
arr[currentSize - 1] = temp;
}
}
}

let array = ["1","2","3","4"];
permutation(array, array.length);

console.log(result);
// use result here

Keep in mind that this is very computationally expensive, with complexity of O(n!).

How do array permutation splitting by (n)

You can try this one:

Script:

function test() {
var array = [1, 4, 5, 8];

console.log(getCombinations(array, 3))
}

function getCombinations(chars, len) {
var result = [];
var f = function(prefix, chars) {
for (var i = 0; i < chars.length; i++) {
var elem = [...prefix, chars[i]];
if(elem.length == len)
result.push(elem);
f(elem, chars.slice(i + 1));
}
}
f([], chars);
return result;
}

Output:

output

JavaScript - Generating all permutations of an array

Not sure if this is the best way, but it seems to work.

@Nina's solution looks good, but it does a fair bit of array concat & slice, so this might work better for larger sets as it avoids that. I do use an object for duplicate checking, but hashmaps are super fast in JS.

Just curious, so did a performance test.
Doing [1,2,3,4,5,6,7], using @Nina's solution take's 38.8 seconds,.
Doing mine toke 175ms.. So the array concat / slice is a massive performance hit, and the marked duplicate will have the same issue. Just something to be aware off.

var ar1 = [2, 5];var ar2 = [1, 2, 3];
function combo(c) { var r = [], len = c.length; tmp = []; function nodup() { var got = {}; for (var l = 0; l < tmp.length; l++) { if (got[tmp[l]]) return false; got[tmp[l]] = true; } return true; } function iter(col,done) { var l, rr; if (col === len) { if (nodup()) { rr = []; for (l = 0; l < tmp.length; l++) rr.push(c[tmp[l]]); r.push(rr); } } else { for (l = 0; l < len; l ++) { tmp[col] = l; iter(col +1); } } } iter(0); return r;}
console.log(JSON.stringify(combo(ar1)));console.log(JSON.stringify(combo(ar2)));console.log('something bigger [1,2,3,4,5,6,7]');console.time('t1');combo([1,2,3,4,5,6,7]);console.timeEnd('t1');


Related Topics



Leave a reply



Submit