Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F8393511
heaps-permute.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
602 B
Subscribers
None
heaps-permute.js
View Options
// http://dsernst.com/2014/12/14/heaps-permutation-algorithm-in-javascript/
function
swap
(
array
,
pos1
,
pos2
)
{
let
temp
=
array
[
pos1
];
array
[
pos1
]
=
array
[
pos2
];
array
[
pos2
]
=
temp
;
}
export
function
heapsPermute
(
array
,
output
,
n
)
{
n
=
n
||
array
.
length
;
// set n default to array.length
if
(
n
===
1
)
{
output
(
array
);
}
else
{
for
(
let
i
=
1
;
i
<=
n
;
i
+=
1
)
{
heapsPermute
(
array
,
output
,
n
-
1
);
let
j
;
if
(
n
%
2
)
{
j
=
1
;
}
else
{
j
=
i
;
}
swap
(
array
,
j
-
1
,
n
-
1
);
// -1 to account for javascript zero-indexing
}
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Jun 4, 7:14 PM (5 d, 4 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3337691
Attached To
R65 Staging repository
Event Timeline
Log In to Comment