Skip to content

Commit b197e8b

Browse files
committed
Minor doc updates
1 parent 4f6fe7c commit b197e8b

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ take an async comparator function for this reason.
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.
16+
// It can use any criteria for comparison, including user input, this is just a simple example:
1617
const comp :Comparator<string> = async ([a, b]) => a > b ? 0 : 1
1718

1819
// Sort five items in ascending order with a maximum of only seven comparisons:
@@ -22,11 +23,18 @@ const sorted = await mergeInsertionSort(['D', 'A', 'B', 'E', 'C'], comp)
2223
### References
2324

2425
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>
26+
The American Mathematical Monthly, 66(5), 387-389. <https://doi.org/10.1080/00029890.1959.11989306>
2627
2. Knuth, D. E. (1998). The Art of Computer Programming: Volume 3: Sorting and Searching (2nd ed.).
2728
Addison-Wesley. <https://cs.stanford.edu/~knuth/taocp.html#vol3>
2829
3. <https://en.wikipedia.org/wiki/Merge-insertion_sort>
2930

31+
See Also
32+
--------
33+
34+
* Python version: <https://pypi.org/project/merge-insertion/>
35+
36+
* This algorithm in action: <https://haukex.github.io/pairrank/> (select "Efficient")
37+
3038
## Type Aliases
3139

3240
### Comparable
@@ -108,7 +116,7 @@ The type of the items to sort.
108116

109117
readonly `T`[]
110118

111-
Array of to sort. Duplicate items are not allowed.
119+
Array to sort. **Duplicate items are not allowed.**
112120

113121
##### comparator
114122

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "merge-insertion",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "The merge-insertion sort (aka the Ford-Johnson algorithm) is optimized for using few comparisons.",
55
"author": {
66
"name": "Hauke Dämpfling",
@@ -9,6 +9,7 @@
99
"license": "ISC",
1010
"repository": { "type": "git", "url": "https://github.com/haukex/merge-insertion.js.git" },
1111
"bugs": { "url": "https://github.com/haukex/merge-insertion.js/issues" },
12+
"keywords": [ "merge-insertion-sort", "ford-johnson" ],
1213
"type": "module",
1314
"main": "dist/merge-insertion.js",
1415
"types": "dist/merge-insertion.d.ts",

src/merge-insertion.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* import { mergeInsertionSort, Comparator } from 'merge-insertion'
1414
*
1515
* // A Comparator must return 0 if the first item is larger, or 1 if the second item is larger.
16+
* // It can use any criteria for comparison, including user input, this is just a simple example:
1617
* const comp :Comparator<string> = async ([a, b]) => a > b ? 0 : 1
1718
*
1819
* // Sort five items in ascending order with a maximum of only seven comparisons:
@@ -27,6 +28,13 @@
2728
* Addison-Wesley. <https://cs.stanford.edu/~knuth/taocp.html#vol3>
2829
* 3. <https://en.wikipedia.org/wiki/Merge-insertion_sort>
2930
*
31+
* See Also
32+
* --------
33+
*
34+
* * Python version: <https://pypi.org/project/merge-insertion/>
35+
*
36+
* * This algorithm in action: <https://haukex.github.io/pairrank/> (select "Efficient")
37+
*
3038
* Author, Copyright and License
3139
* -----------------------------
3240
*
@@ -77,7 +85,7 @@ export function* _groupSizes() :Generator<number, never, never> {
7785
}
7886

7987
/** Helper function to group and reorder items to be inserted via binary search.
80-
* See also the description in the code of {@link mergeInsertionSort}.
88+
* See also the description within the code of {@link mergeInsertionSort}.
8189
* @internal */
8290
export function _makeGroups<T>(array :ReadonlyArray<T>) :[origIdx :number, item :T][] {
8391
const items :ReadonlyArray<[number, T]> = array.map((e,i) => [i, e])
@@ -124,6 +132,7 @@ export async function _binInsertIdx<T extends Comparable>(array :ReadonlyArray<T
124132
* @returns A Promise resolving to a shallow copy of the array sorted in ascending order.
125133
*/
126134
export async function mergeInsertionSort<T extends Comparable>(array :ReadonlyArray<T>, comparator :Comparator<T>) :Promise<T[]> {
135+
// Special cases and error checking
127136
if (array.length<1) return []
128137
if (array.length==1) return Array.from(array)
129138
if (array.length != new Set(array).size) throw new Error('array may not contain duplicate items')
@@ -213,7 +222,7 @@ export async function mergeInsertionSort<T extends Comparable>(array :ReadonlyAr
213222
else {
214223
// Locate the pair we're about to insert in the main chain, to limit the extent of the binary search (see also explanation above).
215224
const pairIdx = mainChain.findIndex(v => Object.is(v, pair))
216-
// Locate the index in the main chain where the pair's smaller item needs to be inserted, and insert it.
225+
// Locate the index in the main chain where the pair's smaller item needs to be inserted.
217226
return [pair.smaller, await _binInsertIdx(mainChain.slice(0,pairIdx).map(p => p.item), pair.smaller, comparator)]
218227
}
219228
})()
@@ -235,7 +244,7 @@ export async function mergeInsertionSort<T extends Comparable>(array :ReadonlyAr
235244
*/
236245
export function mergeInsertionMaxComparisons(n :number) :number {
237246
if (n<0) throw new Error('must specify zero or more items')
238-
// formulas from https://en.wikipedia.org/wiki/Merge-insertion_sort (both of the following work):
247+
// Formulas from https://en.wikipedia.org/wiki/Merge-insertion_sort (both of the following work):
239248
/*let C = 0
240249
for (let i=1; i<=n; i++)
241250
C += Math.ceil(Math.log2((3*i)/4))

0 commit comments

Comments
 (0)