-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpermutation.js
59 lines (47 loc) · 1.34 KB
/
permutation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
var utils = require('./utils.js');
//------------------------------
function _permutationWithCallback(l, off, callback) {
if (off == l.length) {
callback(l);
return;
}
var t;
for (var i = off; i < l.length; ++i) {
t = l[off]; l[off] = l[i]; l[i] = t;
_permutationWithCallback(l, off + 1, callback);
t = l[off]; l[off] = l[i]; l[i] = t;
}
}
function permutationWithCallback(l, callback) {
_permutationWithCallback(l, 0, callback);
}
//------------------------------
function nextPermutation(l) {
var off = l.length - 2;
while (off >= 0 && l[off] > l[off + 1]) --off;
if (off < 0) return false;
var i = off + 1;
while (l[i] > l[off] && i < l.length) ++i;
--i;
var t = l[off]; l[off] = l[i]; l[i] = t;
var j;
for (i = off + 1, j = l.length - 1; i < j; ++i, --j) {
t = l[i]; l[i] = l[j]; l[j] = t;
}
return true;
}
//------------------------------
utils.timeIt('permutationWithCallback', function() {
var l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var count = 0;
permutationWithCallback(l, function() { ++count;});
console.log(count);
});
utils.timeIt('nextPermutation', function() {
var l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var count = 0;
do ++count;
while(nextPermutation(l));
console.log(count);
});