Skip to content

Commit 4a72d08

Browse files
committed
Tweaked tests, docs, and fisherYates
`fisherYates` no longer uses `xorshift32` by default, and the tests now only use printable ASCII for debugging convenience. `fisherYates` is not part of the public API, so the change shouldn't affect users.
1 parent 37f490a commit 4a72d08

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/__tests__/merge-insertion.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ describe('fisherYates', () => {
273273
fisherYates(a, function* () { yield* [5, 1, 5, 0, 2, 2, 0]; throw new Error('no more values') }())
274274
expect(a).toStrictEqual(['G','E','D','C','A','H','B','F'])
275275
})
276-
test('Using default random generator', () => {
276+
test('Using xorshift32', () => {
277277
const a = ['A','B','C']
278-
fisherYates(a)
278+
fisherYates(a, xorshift32())
279279
expect(a).not.toStrictEqual(['A','B','C'])
280280
})
281281
})
@@ -307,9 +307,10 @@ describe('mergeInsertionSort', () => {
307307
])
308308
})
309309

310-
test.each( Array.from({ length: 101 }, (_, i) => ({ len: i })) )('array length $len', async ({len}) => {
311-
const array :Readonly<string[]> = Array.from({ length: len }, (_,i) => String.fromCharCode(65 + i))
310+
test.each( Array.from({ length: 95 }, (_, i) => ({ len: i })) )('array length $len', async ({len}) => {
311+
const array :Readonly<string[]> = Array.from({ length: len }, (_,i) => String.fromCharCode(33 + i)) // printable ASCII
312312
const a = Array.from(array)
313+
const rand = xorshift32()
313314
try {
314315
// in order array
315316
expect( await mergeInsertionSort(a, testComp(comp, mergeInsertionMaxComparisons(len))) ).toStrictEqual(array)
@@ -319,7 +320,7 @@ describe('mergeInsertionSort', () => {
319320
expect( await mergeInsertionSort(a, testComp(comp, mergeInsertionMaxComparisons(len))) ).toStrictEqual(array)
320321
// several shuffles
321322
for (let i=0;i<100;i++) {
322-
fisherYates(a)
323+
fisherYates(a, rand)
323324
expect( await mergeInsertionSort(a, testComp(comp, mergeInsertionMaxComparisons(len))) ).toStrictEqual(array)
324325
}
325326
} catch (ex) {

src/merge-insertion.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* ```typescript
1313
* import { mergeInsertionSort, Comparator } from 'merge-insertion'
1414
*
15-
* // A Comparator should return 0 if the first item is larger, or 1 if the second item is larger.
15+
* // A Comparator must return 0 if the first item is larger, or 1 if the second item is larger.
1616
* const comp :Comparator<string> = async ([a, b]) => a > b ? 0 : 1
1717
*
1818
* // Sort five items in ascending order with a maximum of only seven comparisons:
@@ -22,7 +22,7 @@
2222
* ### References
2323
*
2424
* 1. Ford, L. R., & Johnson, S. M. (1959). A Tournament Problem.
25-
* The American Mathematical Monthly, 66(5), 387389. <https://doi.org/10.1080/00029890.1959.11989306>
25+
* The American Mathematical Monthly, 66(5), 387-389. <https://doi.org/10.1080/00029890.1959.11989306>
2626
* 2. Knuth, D. E. (1998). The Art of Computer Programming: Volume 3: Sorting and Searching (2nd ed.).
2727
* Addison-Wesley. <https://cs.stanford.edu/~knuth/taocp.html#vol3>
2828
* 3. <https://en.wikipedia.org/wiki/Merge-insertion_sort>
@@ -289,9 +289,8 @@ export function* xorshift32() :Generator<number, never, never> {
289289
* 2. Durstenfeld, R. (1964). Algorithm 235: Random permutation. Communications of the ACM, 7(7), 420. doi:10.1145/364520.364540
290290
*
291291
* @param random Generator that returns random integers in the range `0` (inclusive) and `>= array.length-1`.
292-
* The default is {@link xorshift32} - which may not return integers big enough for very large arrays!
293292
* @internal */
294-
export function fisherYates(array :unknown[], random :Generator<number> = xorshift32()) {
293+
export function fisherYates(array :unknown[], random :Generator<number>) {
295294
for (let i=array.length-1; i>0; i--) {
296295
const j = random.next().value % array.length;
297296
[array[i], array[j]] = [array[j], array[i]]

0 commit comments

Comments
 (0)