|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>JSDoc: Source: combinatorics/permutations.js</title> |
| 6 | + |
| 7 | + <script src="scripts/prettify/prettify.js"> </script> |
| 8 | + <script src="scripts/prettify/lang-css.js"> </script> |
| 9 | + <!--[if lt IE 9]> |
| 10 | + <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> |
| 11 | + <![endif]--> |
| 12 | + <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> |
| 13 | + <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> |
| 14 | +</head> |
| 15 | + |
| 16 | +<body> |
| 17 | + |
| 18 | +<div id="main"> |
| 19 | + |
| 20 | + <h1 class="page-title">Source: combinatorics/permutations.js</h1> |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + <section> |
| 28 | + <article> |
| 29 | + <pre class="prettyprint source linenums"><code>(function (exports) { |
| 30 | + 'use strict'; |
| 31 | + var permutations = (function () { |
| 32 | + |
| 33 | + var res; |
| 34 | + |
| 35 | + function swap(arr, i, j) { |
| 36 | + var temp = arr[i]; |
| 37 | + arr[i] = arr[j]; |
| 38 | + arr[j] = temp; |
| 39 | + } |
| 40 | + |
| 41 | + function permutations(arr, current) { |
| 42 | + if (current >= arr.length) { |
| 43 | + return res.push(arr.slice()); |
| 44 | + } |
| 45 | + for (var i = current; i < arr.length; i += 1) { |
| 46 | + swap(arr, i, current); |
| 47 | + permutations(arr, current + 1); |
| 48 | + swap(arr, i, current); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Finds all the permutations of given array.<br><br> |
| 54 | + * Permutation relates to the act of rearranging, or permuting, |
| 55 | + * all the members of a set into some sequence or order. |
| 56 | + * For example there are six permutations of the set {1,2,3}, namely: |
| 57 | + * (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1).<br><br> |
| 58 | + * Complexity: O(N*N!). |
| 59 | + * |
| 60 | + * @example |
| 61 | + * |
| 62 | + * var permutations = require('path-to-algorithms/src/' + |
| 63 | + * 'combinatorics/permutations').permutations; |
| 64 | + * var result = permutations(['apple', 'orange', 'pear']); |
| 65 | + * |
| 66 | + * // [ [ 'apple', 'orange', 'pear' ], |
| 67 | + * // [ 'apple', 'pear', 'orange' ], |
| 68 | + * // [ 'orange', 'apple', 'pear' ], |
| 69 | + * // [ 'orange', 'pear', 'apple' ], |
| 70 | + * // [ 'pear', 'orange', 'apple' ], |
| 71 | + * // [ 'pear', 'apple', 'orange' ] ] |
| 72 | + * console.log(result); |
| 73 | + * |
| 74 | + * @module combinatorics/permutations |
| 75 | + * @public |
| 76 | + * @param {Array} arr Array to find the permutations of. |
| 77 | + * @param {Number} current Current element. |
| 78 | + * @returns {Array} Array containing all the permutations. |
| 79 | + */ |
| 80 | + return function (arr) { |
| 81 | + res = []; |
| 82 | + permutations(arr, 0); |
| 83 | + var temp = res; |
| 84 | + // Free the extra memory |
| 85 | + res = null; |
| 86 | + return temp; |
| 87 | + }; |
| 88 | + }()); |
| 89 | + |
| 90 | + exports.permutations = permutations; |
| 91 | + |
| 92 | +}((typeof window === 'undefined') ? module.exports : window)); |
| 93 | +</code></pre> |
| 94 | + </article> |
| 95 | + </section> |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +</div> |
| 101 | + |
| 102 | +<nav> |
| 103 | + <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-combinatorics_cartesianproduct.html">combinatorics/cartesianproduct</a></li><li><a href="module-combinatorics_combinations.html">combinatorics/combinations</a></li><li><a href="module-combinatorics_permutations.html">combinatorics/permutations</a></li><li><a href="module-combinatorics_variations-repetition.html">combinatorics/variations-repetition</a></li><li><a href="module-data-structures_binary-search-tree.html">data-structures/binary-search-tree</a></li><li><a href="module-data-structures_heap.html">data-structures/heap</a></li><li><a href="module-data-structures_interval-tree.html">data-structures/interval-tree</a></li><li><a href="module-data-structures_linked-list.html">data-structures/linked-list</a></li><li><a href="module-data-structures_red-black-tree.html">data-structures/red-black-tree</a></li><li><a href="module-graphs_others_topological-sort.html">graphs/others/topological-sort</a></li><li><a href="module-graphs_searching_bfs.html">graphs/searching/bfs</a></li><li><a href="module-graphs_searching_dfs.html">graphs/searching/dfs</a></li><li><a href="module-graphs_shortest-path_bellman-ford.html">graphs/shortest-path/bellman-ford</a></li><li><a href="module-graphs_shortest-path_dijkstra.html">graphs/shortest-path/dijkstra</a></li><li><a href="module-graphs_shortest-path_floyd-warshall.html">graphs/shortest-path/floyd-warshall</a></li><li><a href="module-graphs_spanning-trees_prim.html">graphs/spanning-trees/prim</a></li><li><a href="module-primes_is-prime.html">primes/is-prime</a></li><li><a href="module-primes_prime-factor-tree.html">primes/prime-factor-tree</a></li><li><a href="module-primes_sieve-of-eratosthenes.html">primes/sieve-of-eratosthenes</a></li></ul><h3>Classes</h3><ul><li><a href="module-data-structures_binary-search-tree.BinaryTree.html">BinaryTree</a></li><li><a href="module-data-structures_binary-search-tree.Node.html">Node</a></li><li><a href="module-data-structures_heap.Heap.html">Heap</a></li><li><a href="module-data-structures_interval-tree.IntervalTree.html">IntervalTree</a></li><li><a href="module-data-structures_interval-tree.Node.html">Node</a></li><li><a href="module-data-structures_linked-list.LinkedList.html">LinkedList</a></li><li><a href="module-data-structures_linked-list.Node.html">Node</a></li><li><a href="module-data-structures_red-black-tree.RBTree.html">RBTree</a></li><li><a href="module-graphs_shortest-path_bellman-ford.Edge.html">Edge</a></li><li><a href="module-graphs_spanning-trees_prim.Edge.html">Edge</a></li><li><a href="module-graphs_spanning-trees_prim.Graph.html">Graph</a></li><li><a href="module-graphs_spanning-trees_prim.Vertex.html">Vertex</a></li></ul> |
| 104 | +</nav> |
| 105 | + |
| 106 | +<br class="clear"> |
| 107 | + |
| 108 | +<footer> |
| 109 | + Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha13</a> on Sun Jan 11 2015 20:12:49 GMT+0200 (EET) |
| 110 | +</footer> |
| 111 | + |
| 112 | +<script> prettyPrint(); </script> |
| 113 | +<script src="scripts/linenumber.js"> </script> |
| 114 | +</body> |
| 115 | +</html> |
0 commit comments