Package Exports
- @fsad-labs/sortzilla
Readme
๐ข @fsad-labs/sortzilla
Installation
npm install @fsad-labs/sortzilla
# or
yarn add @fsad-labs/sortzillaSupported Algorithms
- Bubble Sort
- Insertion Sort
- Selection Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Counting Sort
Usage
Each algorithm is implemented as a class.
You can create an instance and call the sort method.
Example with Numbers
import { bubbleSort, quickSort } from '@fsad-labs/sortzilla';
const result = bubbleSort({ array: [5, 3, 8, 1] });
console.log([...result]); // [1, 3, 5, 8]
// Reuse helper functions
console.log(result.byDesc()); // [8, 5, 3, 1]
console.log(result.byAsc()); // [1, 3, 5, 8]Example with Strings
import { selectionSort } from '@fsad-labs/sortzilla';
const result = selectionSort({
array: ['z', 'a', 'd', 'b'],
ascending: false,
});
console.log([...result]); // ["z", "d", "b", "a"]Example with Objects
import { mergeSort } from '@fsad-labs/sortzilla';
type User = { id: number; name: string };
const result = mergeSort({
array: [
{ id: 3, name: 'Alice' },
{ id: 1, name: 'Bob' },
{ id: 2, name: 'Charlie' },
],
field: 'id',
ascending: true,
});
console.log([...result]);
// [{ id: 1, name: "Bob" }, { id: 2, name: "Charlie" }, { id: 3, name: "Alice" }]API
Method
function bubbleSort<T extends SortTypes>(props: SortProps<T>);Params Options
sort(props: SortProps)
type SortTypes = string | number | object;
interface SortProps<T extends SortTypes> {
array: T[];
field?: T extends object ? keyof T : undefined;
ascending?: boolean;
}--props:::
arrayโ input array (number[] | string[] | object[]).fieldโ (optional) field key if sorting objects.ascending(boolean, default: true) โ sort order.
QuickSort accepts an additional option:
type QuickSortProp = SortProps<T> & { ipivot?: number };ipivot(number, default: -1) โ index for pivot selection (-1 = random).
Returns a sorted array with extra helpers:
interface IOrderSort<T> {
byDesc(): T[];
byAsc(): T[];
}Example Switching Order Without Re-Sorting
import { insertionSort } from '@fsad-labs/sortzilla';
const result = insertionSort({ array: [4, 2, 7, 1] });
console.log([...result]); // [1, 2, 4, 7]
console.log(result.byDesc()); // [7, 4, 2, 1]๐ค Contributing
- Fork this repository
- Create a feature branch (
git checkout -b feat/my-feature) - Commit changes (
git commit -m 'Add new feature') - Push and create a Pull Request
If my work has helped you or saved you some time, consider Buy Me a Coffeeโ. It keeps me energized and motivated to keep creating and improving.
๐ License
This project is licensed under the MIT License ยฉ drixev