|
1 | 1 | /**
|
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 |
3 | 21 | */
|
4 |
| - |
5 | 22 | const _fisheryates = (randint) => (n, a, i, j) => {
|
6 | 23 | // We will swap at most n elements
|
7 | 24 | // NOTE: When n = j - i, the last swap swaps a[j-1] with itself,
|
|
0 commit comments