User Tools

Site Tools


algorithms

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
algorithms [2011-06-19 00:21 CEST]
dominik
algorithms [2011-11-17 19:50 CET] (current)
dominik Array permute
Line 30: Line 30:
 printf('​%d days %02dh %02dm %02ds',​ $d, $h, $m, $s); printf('​%d days %02dh %02dm %02ds',​ $d, $h, $m, $s);
 </​code>​ </​code>​
 +
 +===== Array permute =====
 +
 +<code javascript>​
 +//+ Jonas Raoni Soares Silva
 +//@ http://​jsfromhell.com/​array/​permute [rev. #1]
 +permute = function(v, m){
 +    for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i);
 +    for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
 +        ++i < l; x[2][i] = i, x[1][i] = x[0][i] = j /= --k);
 +    for(r = new Array(q); ++p < q;)
 +        for(r[p] = new Array(l), i = -1; ++i < l; !--x[1][i] && (x[1][i] = x[0][i],
 +            x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : v[x[3][i]])
 +            for(x[3][i] = x[2][i], f = 0; !f; f = !f)
 +                for(j = i; j; x[3][--j] == x[2][i] && (x[3][i] = x[2][i] = (x[2][i] + 1) % l, f = 1));
 +    return r;
 +};
 +</​code>​
 +
algorithms.txt · Last modified: 2011-11-17 19:50 CET by dominik