Skip to content

Commit 21c8bdd

Browse files
📚 docs(_fisheryates): Add references.
1 parent 0dd55b3 commit 21c8bdd

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/kernel/_fisheryates.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
/**
2-
* Sample array using Fisher-Yates method.
2+
* Sample element from an array using Fisher-Yates method.
3+
*
4+
* NOTE: The original description of the algorithm by Fisher and Yates [1] had
5+
* unnecessary bookkeeping which made the algorithm run in O(n * (j-i)) time.
6+
* This implementation follows Durstenfeld's [2] and Knuth's [3] descriptions
7+
* which yield O(n) running time.
8+
*
9+
* For more information see the excellent "Fisher–Yates shuffle" page on
10+
* Wikipedia [4]. Fisher and Yates description is referred there as
11+
* "Fisher and Yate's original method" or "pencil-and-paper method".
12+
* The more efficient implementation described by Durstenfeld and Knuth is
13+
* referred there as "the modern method".
14+
*
15+
* [1] Fisher, Ronald A.; Yates, Frank (1938). Statistical tables for
16+
* biological, agricultural and medical research.
17+
* [2] Durstenfeld, R. (July 1964). "Algorithm 235: Random permutation"
18+
* [3] Knuth, Donald E. (1969). Seminumerical algorithms. The Art of Computer
19+
* Programming Volume 2.
20+
* [4] https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
321
*/
4-
522
const _fisheryates = (randint) => (n, a, i, j) => {
623
// We will swap at most n elements
724
// NOTE: When n = j - i, the last swap swaps a[j-1] with itself,

0 commit comments

Comments
 (0)