Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-ravens-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sortman": patch
---

Add bucket sort
5 changes: 5 additions & 0 deletions .changeset/open-yaks-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sortman": patch
---

Change prepublishOnly script
5 changes: 5 additions & 0 deletions .changeset/real-jeans-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sortman": patch
---

Add bucket sort test and benchmark
5 changes: 5 additions & 0 deletions .changeset/wide-flies-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sortman": patch
---

Fix default sort
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
28 changes: 28 additions & 0 deletions src/Sort/BucketSort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { SortBase, type SortCoreElement } from "./SortBase";

export class BucketSort extends SortBase {
public core<T>(content: SortCoreElement<T>): SortCoreElement<T> {
const arr = content.concat();
const bucket: SortCoreElement<T>[] = [];

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<T> = [];

for (let i = 0; i < bucket.length; i++) {
const elements = bucket[i];
if (elements) {
result.push(...elements);
}
}

return result;
}
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -23,7 +25,7 @@ export class SortMan {
public static sort<T>(arr: T[], options: SortOptions<T>): T[];
public static sort<T>(arr: T[], options?: SortOptions<T>): T[] {
try {
return this.merge.sort(arr, options as SortOptions<T>);
return this.quick.sort(arr, options as SortOptions<T>);
} catch {
return this.insertion.sort(arr, options as SortOptions<T>);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,9 @@ export const sorts: Sorts = [
{
name: "Shell sort",
algorithm: SortMan.shell
},
{
name: "Bucket sort",
algorithm: SortMan.bucket
}
];