GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript/Backtracking/GeneratePermutations.js at master · Randomlyclueless/JavaScript Expand file tree/ Copy pathGeneratePermutations.jsMore file actions 36 lines (31 loc) · 851 Bytes / Copy pathGeneratePermutations.jsFile metadata and controls36 lines (31 loc) · 851 Bytes 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 /* * Problem Statement: Generate all distinct permutations of an array (all permutations should be in sorted order); * * What is permutations? * - Permutation means possible arrangements in a set (here it is an array); * * Reference to know more about permutations: * - https://www.britannica.com/science/permutation * */ const swap = (arr, i, j) => { const newArray = [...arr] ;[newArray[i], newArray[j]] = [newArray[j], newArray[i]] // Swapping elements ES6 way return newArray } const permutations = (arr) => { const P = [] const permute = (arr, low, high) => { if (low === high) { P.push([...arr]) return P } for (let i = low; i <= high; i++) { arr = swap(arr, low, i) permute(arr, low + 1, high) } return P } return permute(arr, 0, arr.length - 1) } export { permutations } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |