- π Lightweight and fast
- π΄ ES3-compliant
- π Comes with built-in TypeScript definitions
- π Split into files (under
cjs/) to import what's needed - π» Portable between the browser and Node.js
This is a library of sorting algorithms written in TypeScript. Currently, the library includes the following algorithms:
- Bubble sort
- Insertion sort
- Selection sort
- Merge sort
- Quick sort
- Bogo sort
- Radix sort
- Heap sort
- Counting sort
- Shell sort
Thanks to @codediodeio for the idea, taken from his video about sorting algorithms and its corresponding repo.
- Via NPM:
npm install @santi100/sorting-lib - Via Yarn:
yarn add @santi100/sorting-lib - Via PNPM:
pnpm install @santi100/sorting-lib
-
type SortComparator<T = unknown> = (a: T, b: T) => number;Comparator function for every sorting algorithm, except forradixSort. It's fully compatible withArray.prototype.sort's callback. -
type SortOrder = 'ascending' | 'descending';Sorting order string. Must be eitherascendingordescending. -
interface SortOptions<T = unknown>;Shape of theoptsobject passed to every sorting algorithm, except forradixSort. SeeRadixSortOptionsfor the options specific to it.comparator?: SortComparator<T>;Comparator function for every sorting algorithm, except forradixSort. It's fully compatible withArray.prototype.sort's callback. SeeSortComparator.order?: SortOrder;Sorting order string. Must be eitherascendingordescending. SeeSortOrder.
-
interface RadixSortOptions;Shape of theoptsobject exclusive toradixSort.order?: SortOrder;Sorting order string. Must be eitherascendingordescending. SeeSortOrder.
-
type CountingSortOptions = RadixSortOptions;(since 0.0.3) Shape of theoptsobject exclusive tocountingSort. -
function bubbleSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];Sortsarrwith bubble-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity: Quadratic ($O(n ^ 2)$).
function insertionSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];Sortsarrwith insertion-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity (average and worst-case): Quadratic ($O(n ^ 2)$).
Time complexity (best-case): Linear ($O(n)$).
function selectionSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];Sortsarrwith selection-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity (average and worst-case): Quadratic ($O(n ^ 2)$).
function mergeSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];Sortsarrwith merge-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity (average and worst-case): Quasi-linear ($O(n \log{n})$).
function quickSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];Sortsarrwith quick-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity (best and average): Quasi-linear ($O(n \log{n})$).
Time complexity (worst): Quadratic ($ O(n ^ 2) $).
function bogoSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];Sortsarrwith bogo-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity (average): Linear-factorial ($O((n!)(n))$).
Worst-case time complexity: Infinity ($O(β)$).
function radixSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];Sortsarrwith radix-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity (best, average and worst):
function heapSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];Sortsarrwith heap-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity (best, average and worst): Quasi-linear ($O(n \log {n})$).
function shellSort<T = unknown>(arr: T[], opts?: SortOptions<T>): T[];: Sortsarrwith shell-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity: Depends on the gap sequence used. Best known is
function countingSort(arr: number[], opts?: CountingSortOptions): T[];: Sortsarrwith counting-sort and returns a new sorted array (i.e.: doesn't mutatearr). It takes the array to sort, and optional sorting options, and returns a sorted copy ofarr.
Time complexity (best, average and worse):
-
function bozoSort<T = unknown>(array: T[]): T[];Sorts an array using the Bozo Sort algorithm.Name Description arrayThe array to be sorted. -
function bozoSort<T = unknown>(array: T[], opts: SortingOptions<T>): T[];Sorts an array using the Bozo Sort algorithm.Name Description arrayThe array to be sorted. optsAn object containing sorting options.
import { mergeSort, bozoSort } from '@santi100/sorting-lib'; // ESM
const { mergeSort, bozoSort } = require('@santi100/sorting-lib'); // CJS
const sorted = mergeSort([4, 2, 5, 1, 3]); // sorted = [1, 2, 3, 4, 5]
const descendingSorted = mergeSort([4, 2, 5, 1, 3], { order: 'descending' }); // descendingSorted = [5, 4, 3, 2, 1]
const objSorted = mergeSort(
[
{
age: 23
},
{
age: 12
},
{
age: 30
}
],
{ comparator: (a, b) => a.age - b.age }
); // returns [ { age: 12 }, { age: 23 }, { age: 30 }]
// You can do same for all algorithms, except for `radixSort`, which is limited to ints for now, so
// its only option is `order`.
const sortedArray = bozoSort(array);
console.log(sortedArray); // Output: [1, 2, 3]Wanna contribute? File an issue or pull request! Look at the contribution instructions and make sure you follow the contribution Code of Conduct.
*Hasn't been tested in an actual ES3 environment. Feel free to open an issue or pull request if you find any non-ES3 thing. See "Contribute" for instructions on how to do so.
^The source code is just a few kilobytes in size.