diff --git a/.changeset/old-ravens-rule.md b/.changeset/old-ravens-rule.md new file mode 100644 index 0000000..9895bd4 --- /dev/null +++ b/.changeset/old-ravens-rule.md @@ -0,0 +1,5 @@ +--- +"sortman": patch +--- + +Add bucket sort diff --git a/.changeset/open-yaks-deny.md b/.changeset/open-yaks-deny.md new file mode 100644 index 0000000..b8dcbf3 --- /dev/null +++ b/.changeset/open-yaks-deny.md @@ -0,0 +1,5 @@ +--- +"sortman": patch +--- + +Change prepublishOnly script diff --git a/.changeset/real-jeans-enjoy.md b/.changeset/real-jeans-enjoy.md new file mode 100644 index 0000000..82285d1 --- /dev/null +++ b/.changeset/real-jeans-enjoy.md @@ -0,0 +1,5 @@ +--- +"sortman": patch +--- + +Add bucket sort test and benchmark diff --git a/.changeset/wide-flies-repair.md b/.changeset/wide-flies-repair.md new file mode 100644 index 0000000..0a309e2 --- /dev/null +++ b/.changeset/wide-flies-repair.md @@ -0,0 +1,5 @@ +--- +"sortman": patch +--- + +Fix default sort diff --git a/package.json b/package.json index 3c2c617..bdc334e 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "check": "npx tsc", "format": "prettier --write .", "lint": "prettier --check . && eslint .", - "prepublishOnly": "npm run changesets && npm run build", + "prepublishOnly": "npm run build", "changesets": "npx @changesets/cli", "changesets:version": "npx @changesets/cli version", "benchmark": "npm-run-all benchmark:*", diff --git a/src/Sort/BucketSort.ts b/src/Sort/BucketSort.ts new file mode 100644 index 0000000..adbd5af --- /dev/null +++ b/src/Sort/BucketSort.ts @@ -0,0 +1,28 @@ +import { SortBase, type SortCoreElement } from "./SortBase"; + +export class BucketSort extends SortBase { + public core(content: SortCoreElement): SortCoreElement { + const arr = content.concat(); + const bucket: SortCoreElement[] = []; + + for (const element of arr) { + const cache = bucket[element.num]; + if (cache) { + bucket[element.num] = cache.concat(element); + } else { + bucket[element.num] = [element]; + } + } + + const result: SortCoreElement = []; + + for (let i = 0; i < bucket.length; i++) { + const elements = bucket[i]; + if (elements) { + result.push(...elements); + } + } + + return result; + } +} diff --git a/src/index.ts b/src/index.ts index bb9a8fe..35df4f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,12 +6,14 @@ import { SelectionSort } from "./Sort/SelectionSort"; import { MergeSort } from "./Sort/MergeSort"; import { HeapSort } from "./Sort/HeapSort"; import { ShellSort } from "./Sort/ShellSort"; +import { BucketSort } from "./Sort/BucketSort"; import type { SortOptions } from "./Sort/SortBase"; export class SortMan { public static readonly bogo = new BogoSort(); public static readonly bubble = new BubbleSort(); + public static readonly bucket = new BucketSort(); public static readonly quick = new QuickSort(); public static readonly merge = new MergeSort(); public static readonly heap = new HeapSort(); @@ -23,7 +25,7 @@ export class SortMan { public static sort(arr: T[], options: SortOptions): T[]; public static sort(arr: T[], options?: SortOptions): T[] { try { - return this.merge.sort(arr, options as SortOptions); + return this.quick.sort(arr, options as SortOptions); } catch { return this.insertion.sort(arr, options as SortOptions); } diff --git a/tests/utils.ts b/tests/utils.ts index afd3c43..5976757 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -90,5 +90,9 @@ export const sorts: Sorts = [ { name: "Shell sort", algorithm: SortMan.shell + }, + { + name: "Bucket sort", + algorithm: SortMan.bucket } ];