|
10 | 10 | arr[j] = temp; |
11 | 11 | } |
12 | 12 |
|
13 | | - /** |
14 | | - * Finds all the permutations of given array. |
15 | | - * Complexity O(n*n!). |
16 | | - * |
17 | | - * @param {Array} arr Array to find the permutations of |
18 | | - * @param {Number} current Current element |
19 | | - * @returns {Array} Array containing all the permutations |
20 | | - */ |
21 | 13 | function permutations(arr, current) { |
22 | 14 | if (current >= arr.length) { |
23 | 15 | return res.push(arr.slice()); |
|
29 | 21 | } |
30 | 22 | } |
31 | 23 |
|
| 24 | + /** |
| 25 | + * Finds all the permutations of given array.<br><br> |
| 26 | + * Permutation relates to the act of rearranging, or permuting, |
| 27 | + * all the members of a set into some sequence or order. |
| 28 | + * For example there are six permutations of the set {1,2,3}, namely: |
| 29 | + * (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1).<br><br> |
| 30 | + * Complexity: O(N*N!). |
| 31 | + * |
| 32 | + * @example |
| 33 | + * |
| 34 | + * var permutations = require('path-to-algorithms/src/' + |
| 35 | + * 'combinatorics/permutations').permutations; |
| 36 | + * var result = permutations(['apple', 'orange', 'pear']); |
| 37 | + * |
| 38 | + * // [ [ 'apple', 'orange', 'pear' ], |
| 39 | + * // [ 'apple', 'pear', 'orange' ], |
| 40 | + * // [ 'orange', 'apple', 'pear' ], |
| 41 | + * // [ 'orange', 'pear', 'apple' ], |
| 42 | + * // [ 'pear', 'orange', 'apple' ], |
| 43 | + * // [ 'pear', 'apple', 'orange' ] ] |
| 44 | + * console.log(result); |
| 45 | + * |
| 46 | + * @module combinatorics/permutations |
| 47 | + * @public |
| 48 | + * @param {Array} arr Array to find the permutations of. |
| 49 | + * @param {Number} current Current element. |
| 50 | + * @returns {Array} Array containing all the permutations. |
| 51 | + */ |
32 | 52 | return function (arr) { |
33 | 53 | res = []; |
34 | 54 | permutations(arr, 0); |
|
41 | 61 |
|
42 | 62 | exports.permutations = permutations; |
43 | 63 |
|
44 | | -}(typeof exports === 'undefined' ? window : exports)); |
| 64 | +}((typeof window === 'undefined') ? module.exports : window)); |
0 commit comments