Skip to content

Commit c00e772

Browse files
committed
Start reworking lib to DENO
1 parent 2844ed2 commit c00e772

File tree

318 files changed

+567
-386
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

318 files changed

+567
-386
lines changed

.gitpod.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This configuration file was automatically generated by Gitpod.
2+
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
3+
# and commit this file to your remote git repository to share the goodness with others.
4+
5+
# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart
6+
7+
tasks:
8+
- init: yarn install && yarn run build
9+
10+

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"deno.enable": true
3+
}

deno.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@g43/utils",
3+
"tasks": {
4+
"bench": "deno bench src/"
5+
},
6+
"version": "0.1.0",
7+
"exports": "./src/index.ts"
8+
}

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
export * from "./GUtils";
2-
File renamed without changes.

src/sort/insertion-sort.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function insertionSort<T>(list: readonly T[], comparator: (a: T, b: T) => number): void {
2+
const listCopy = [...list];
3+
for (let i = 1; i < listCopy.length; i++) {
4+
const item = listCopy[i];
5+
if (comparator(item, listCopy[i - 1]) < 0) {
6+
sortUpLowToHigh(listCopy, i, comparator);
7+
}
8+
}
9+
}
10+
11+
function sortUpLowToHigh<T>(list: T[], i: number, comparator: (a: T, b: T) => number): void {
12+
const item = list[i];
13+
let attemptPos = i - 1;
14+
while (attemptPos !== 0 && comparator(list[attemptPos - 1], item, ) > 0) {
15+
attemptPos--;
16+
}
17+
list.splice(i, 1);
18+
list.splice(attemptPos, 0, item);
19+
}

src/sort/merge-sort.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
let array: any[] = [];
2+
let tempMergeArray: any[] = [];
3+
4+
export function mergeSort<T>(arr: T[], comparator: (a: T, b: T) => number): void {
5+
array = arr;
6+
tempMergeArray = new Array<T>(arr.length);
7+
doMergeSort(0, arr.length - 1, comparator);
8+
}
9+
10+
function doMergeSort<T>(lowerIndex: number, higherIndex: number, comparator: (a: T, b: T) => number): void {
11+
if (lowerIndex >= higherIndex) {
12+
return;
13+
}
14+
const middle = Math.floor(lowerIndex + (higherIndex - lowerIndex) / 2);
15+
doMergeSort(lowerIndex, middle, comparator);
16+
doMergeSort(middle + 1, higherIndex, comparator);
17+
mergeParts(lowerIndex, middle, higherIndex, comparator);
18+
}
19+
20+
function mergeParts<T>(lowerIndex: number, middle: number, higherIndex: number, comparator: (a: T, b: T) => number): void {
21+
for (let index = lowerIndex; index <= higherIndex; index++) {
22+
tempMergeArray[index] = array[index];
23+
}
24+
25+
let i = lowerIndex;
26+
let j = middle + 1;
27+
let k = lowerIndex;
28+
while (i <= middle && j <= higherIndex) {
29+
if (comparator(tempMergeArray[i], tempMergeArray[j]) <= 0) {
30+
array[k] = tempMergeArray[i++];
31+
} else {
32+
array[k] = tempMergeArray[j++];
33+
}
34+
k++;
35+
}
36+
while (i <= middle) {
37+
array[k++] = tempMergeArray[i++];
38+
}
39+
40+
}

src/sort/quick-sort.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
let array: any[] = [];
2+
let tmpValue: any | null = null;
3+
4+
5+
export function quickSort<T>(arr: T[], comparator: (a: T, b: T) => number): void {
6+
if (!arr?.length) {
7+
return;
8+
}
9+
10+
array = arr;
11+
12+
doQuickSort(0, array.length - 1, comparator);
13+
}
14+
15+
function doQuickSort<T>(lowerIndex: number, higherIndex: number, comparator: (a: T, b: T) => number): void {
16+
let i = lowerIndex;
17+
let j = higherIndex;
18+
19+
const pivot = array[~~(lowerIndex + (higherIndex - lowerIndex) / 2)];
20+
while (i <= j) {
21+
while (comparator(array[i], pivot) < 0) {
22+
i++;
23+
}
24+
while (comparator(array[j], pivot) > 0) {
25+
j--;
26+
}
27+
28+
if (i <= j) {
29+
exchangeNumbers(i++, j--);
30+
}
31+
}
32+
33+
if (lowerIndex < j) {
34+
doQuickSort(lowerIndex, j, comparator);
35+
}
36+
if (i < higherIndex) {
37+
doQuickSort(i, higherIndex, comparator);
38+
}
39+
}
40+
41+
function exchangeNumbers(i: number, j: number): void {
42+
tmpValue = array[i];
43+
array[i] = array[j];
44+
array[j] = tmpValue;
45+
}

src/sort/sort.bench.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { quickSort } from "./quick-sort.ts";
2+
import { insertionSort } from "./insertion-sort.ts";
3+
import { mergeSort } from "./merge-sort.ts";
4+
const SIZE = 100000
5+
const randomArray = new Array<number>(SIZE).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER));
6+
const ascArray = new Array<number>(SIZE).fill(0).map((_, i) => i);
7+
const descArray = new Array<number>(SIZE).fill(0).map((_, i) => SIZE - i);
8+
9+
10+
Deno.bench("quickSort", { group: "sort.random" }, () => {
11+
quickSort(randomArray, (a, b) => a - b);
12+
});
13+
Deno.bench("insertionSort", { group: "sort.random" }, () => {
14+
insertionSort(randomArray, (a, b) => a - b);
15+
});
16+
Deno.bench("mergeSort", { group: "sort.random" }, () => {
17+
mergeSort(randomArray, (a, b) => a - b);
18+
});
19+
20+
21+
22+
Deno.bench("quickSort", { group: "sort.asc" }, () => {
23+
quickSort(ascArray, (a, b) => a - b);
24+
});
25+
Deno.bench("insertionSort", { group: "sort.asc" }, () => {
26+
insertionSort(ascArray, (a, b) => a - b);
27+
});
28+
Deno.bench("mergeSort", { group: "sort.asc" }, () => {
29+
mergeSort(ascArray, (a, b) => a - b);
30+
});
31+
32+
Deno.bench("quickSort", { group: "sort.desc" }, () => {
33+
quickSort(descArray, (a, b) => a - b);
34+
});
35+
Deno.bench("insertionSort", { group: "sort.desc" }, () => {
36+
insertionSort(descArray, (a, b) => a - b);
37+
});
38+
Deno.bench("mergeSort", { group: "sort.desc" }, () => {
39+
mergeSort(descArray, (a, b) => a - b);
40+
});
41+

0 commit comments

Comments
 (0)