Finds all the permutations of given array.
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).
Complexity: O(N*N!).
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).
Complexity: O(N*N!).
- Source:
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array
|
Array to find the permutations of. |
Returns:
- Type:
-
Array
Array containing all the permutations.
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);