Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
63 lines (57 loc) · 1.77 KB

File metadata and controls

63 lines (57 loc) · 1.77 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
60
61
62
63
(function (exports) {
'use strict';
var permutations = (function () {
var res;
function swap(arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function permutations(arr, current) {
if (current >= arr.length) {
return res.push(arr.slice());
}
for (var i = current; i < arr.length; i += 1) {
swap(arr, i, current);
permutations(arr, current + 1);
swap(arr, i, current);
}
}
/**
* Finds all the permutations of given array.<br><br>
* Permutation relates to the act of rearranging, or permuting,
* all the members of a set into some sequence or order.
* For example there are six permutations of the set {1,2,3}, namely:
* (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1).<br><br>
* Complexity: O(N*N!).
*
* @example
*
* var permutations = require('path-to-algorithms/src/' +
* 'combinatorics/permutations').permutations;
* var result = permutations(['apple', 'orange', 'pear']);
*
* // [ [ 'apple', 'orange', 'pear' ],
* // [ 'apple', 'pear', 'orange' ],
* // [ 'orange', 'apple', 'pear' ],
* // [ 'orange', 'pear', 'apple' ],
* // [ 'pear', 'orange', 'apple' ],
* // [ 'pear', 'apple', 'orange' ] ]
* console.log(result);
*
* @module combinatorics/permutations
* @public
* @param {Array} arr Array to find the permutations of.
* @returns {Array} Array containing all the permutations.
*/
return function (arr) {
res = [];
permutations(arr, 0);
var temp = res;
// Free the extra memory
res = null;
return temp;
};
}());
exports.permutations = permutations;
}((typeof window === 'undefined') ? module.exports : window));
Morty Proxy This is a proxified and sanitized view of the page, visit original site.