Skip to content

Commit 1cec042

Browse files
✨ feat: First draft.
1 parent fe1c2c0 commit 1cec042

10 files changed

+8462
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[@aureooms/js-insertion-sort](https://aureooms.github.io/js-insertion-sort)
1+
:rewind: [@aureooms/js-insertion-sort](https://aureooms.github.io/js-insertion-sort)
22
==
33

44
Insertion sorting algorithms for JavaScript.

package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"transform-remove-console",
2828
{
2929
"exclude": [
30+
"debug",
3031
"log",
3132
"error",
3233
"warn"
@@ -77,6 +78,8 @@
7778
},
7879
"dependencies": {},
7980
"devDependencies": {
81+
"@aureooms/js-array": "^4.0.0",
82+
"@aureooms/js-in-situ-sort-spec": "^8.0.0",
8083
"@babel/cli": "7.11.6",
8184
"@babel/core": "7.11.6",
8285
"@babel/preset-env": "7.11.5",
@@ -101,7 +104,16 @@
101104
"lib"
102105
],
103106
"homepage": "https://aureooms.github.io/js-insertion-sort",
104-
"keywords": ["agpl", "algorithms", "insertion", "insertion-sort", "javascript", "quadratic-time", "sorting", "sorting-algorithms"],
107+
"keywords": [
108+
"agpl",
109+
"algorithms",
110+
"insertion",
111+
"insertion-sort",
112+
"javascript",
113+
"quadratic-time",
114+
"sorting",
115+
"sorting-algorithms"
116+
],
105117
"license": "AGPL-3.0",
106118
"main": "lib/index.js",
107119
"prettier": {
@@ -114,12 +126,12 @@
114126
"scripts": {
115127
"build": "babel --delete-dir-on-start --env-name production src -d lib",
116128
"cover": "nyc --reporter=lcov npm test",
129+
"dev": "npm run lint -- --fix && npm run cover -- -- -st --fail-fast",
117130
"esdoc": "esdoc",
131+
"lint": "xo",
118132
"prepare": "npm run build",
119133
"release": "np",
120134
"test": "ava",
121-
"lint": "xo",
122-
"dev": "npm run lint -- --fix && npm run cover -- -- -st --fail-fast",
123135
"travis": "npm run lint && npm run cover"
124136
},
125137
"sideEffects": false,

src/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sort from './sort';
2+
import sortTypedDecreasing from './sortTypedDecreasing';
3+
import sortTypedDecreasingOptimized from './sortTypedDecreasingOptimized';
4+
import sortTypedIncreasing from './sortTypedIncreasing';
5+
import sortTypedIncreasingOptimized from './sortTypedIncreasingOptimized';
6+
7+
/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */
8+
export default {
9+
sort,
10+
sortTypedDecreasing,
11+
sortTypedDecreasingOptimized,
12+
sortTypedIncreasing,
13+
sortTypedIncreasingOptimized,
14+
};
15+
16+
export {
17+
sort,
18+
sortTypedDecreasing,
19+
sortTypedDecreasingOptimized,
20+
sortTypedIncreasing,
21+
sortTypedIncreasingOptimized,
22+
};

src/sort.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const sort = (compare, a, i, j) => {
2+
for (let k = i + 1; k < j; ++k) {
3+
let t = k;
4+
const o = a[t];
5+
while (t-- > i && compare(a[t], o) > 0) a[t + 1] = a[t];
6+
a[t + 1] = o;
7+
}
8+
};
9+
10+
export default sort;

src/sortTypedDecreasing.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const sortTypedDecreasing = (a, i, j) => {
2+
for (let k = i + 1; k < j; ++k) {
3+
let t = k;
4+
const o = a[t];
5+
while (t-- > i && a[t] < o) a[t + 1] = a[t];
6+
a[t + 1] = o;
7+
}
8+
};
9+
10+
export default sortTypedDecreasing;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import assert from 'assert';
2+
3+
/**
4+
* Hyp: a[i-1] >= max(a[i:j]).
5+
*/
6+
const sortTypedDecreasingOptimized = (a, i, j) => {
7+
assert(i >= 1);
8+
for (let k = i + 1; k < j; ++k) {
9+
let t = k;
10+
const o = a[t];
11+
while (a[--t] < o) a[t + 1] = a[t];
12+
a[t + 1] = o;
13+
}
14+
};
15+
16+
export default sortTypedDecreasingOptimized;

src/sortTypedIncreasing.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const sortTypedIncreasing = (a, i, j) => {
2+
for (let k = i + 1; k < j; ++k) {
3+
let t = k;
4+
const o = a[t];
5+
while (t-- > i && a[t] > o) a[t + 1] = a[t];
6+
a[t + 1] = o;
7+
}
8+
};
9+
10+
export default sortTypedIncreasing;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import assert from 'assert';
2+
3+
/**
4+
* Hyp: a[i-1] <= min(a[i:j]).
5+
*/
6+
const sortTypedIncreasingOptimized = (a, i, j) => {
7+
assert(i >= 1);
8+
for (let k = i + 1; k < j; ++k) {
9+
let t = k;
10+
const o = a[t];
11+
while (a[--t] > o) a[t + 1] = a[t];
12+
a[t + 1] = o;
13+
}
14+
};
15+
16+
export default sortTypedIncreasingOptimized;

test/src/all.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// eslint-disable-next-line ava/use-test
2+
import ava from 'ava';
3+
4+
import * as spec from '@aureooms/js-in-situ-sort-spec';
5+
import {min, copy} from '@aureooms/js-array';
6+
import {increasing, decreasing} from '@aureooms/js-compare';
7+
import {
8+
sort,
9+
sortTypedIncreasing,
10+
sortTypedDecreasing,
11+
sortTypedIncreasingOptimized,
12+
sortTypedDecreasingOptimized,
13+
} from '../../src';
14+
15+
const sortTyped = (compare, a, i, j) => {
16+
switch (compare) {
17+
case increasing:
18+
return sortTypedIncreasing(a, i, j);
19+
case decreasing:
20+
return sortTypedDecreasing(a, i, j);
21+
default:
22+
throw new Error(
23+
'First argument `compare` should be one of {increasing, decreasing}.',
24+
);
25+
}
26+
};
27+
28+
const sortTypedOptimized = (compare, a, i, j) => {
29+
const X = a.constructor;
30+
const N = j - i;
31+
const b = new X(N + 1);
32+
copy(a, i, j, b, 1);
33+
b[0] = min(compare, a, i, j);
34+
switch (compare) {
35+
case increasing:
36+
sortTypedIncreasingOptimized(b, 1, N + 1);
37+
return copy(b, 1, N + 1, a, i);
38+
case decreasing:
39+
sortTypedDecreasingOptimized(b, 1, N + 1);
40+
return copy(b, 1, N + 1, a, i);
41+
default:
42+
throw new Error(
43+
'First argument `compare` should be one of {increasing, decreasing}.',
44+
);
45+
}
46+
};
47+
48+
spec.test(ava, [
49+
['sort', sort],
50+
['sortTyped', sortTyped],
51+
['sortTypedOptimized', sortTypedOptimized],
52+
]);

0 commit comments

Comments
 (0)