FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javascript-algorithms/src/combinatorics/permutations.js at master · phpsmarter/javascript-algorithms · GitHub
phpsmarter
/
javascript-algorithms
Public
forked from
mgechev/javascript-algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
javascript-algorithms
/
src
/
combinatorics
/
permutations.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
63 lines (57 loc) · 1.77 KB
Breadcrumbs
javascript-algorithms
/
src
/
combinatorics
/
permutations.js
Copy path
File metadata and controls
63 lines (57 loc) · 1.77 KB
Raw
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
)
)
;
Back
|
FazBrowse Home
|
New Git URL