xiaohe0601 / github@xiaohe0601 / gitee@xiaohe0601
English | ็ฎไฝไธญๆ
[!WARNING] ๐จ This is an entertainment project, do not use it in production! The author is not responsible for any losses caused!
- ๐ช A variety of magical sorting algorithms
- ๐ Friendly API design
- ๐ง Full TypeScript support
- ๐งช Complete unit testing
- ๐ No framework restrictions
# pnpm
pnpm add magic-sort
# yarn
yarn add magic-sort
# npm
npm install magic-sort
import { bogoSort } from "magic-sort";
const array = [1, 3, 5, 2, 4];
bogoSort(array); // array -> [1,2,3,4,5]
bogoSort(array, (a, b) => b - a); // array -> [5,4,3,2,1]
"If you let a monkey randomly hit keys on a typewriter for an infinite amount of time, it will almost surely type out any given text, such as the complete works of Shakespeare."
type CompareFunc<T> = (a: T, b: T) => number;
function bogoSort<T>(array: T[], compareFunc?: CompareFunc<T>): T[];
Randomly shuffles the given array. When the number of shuffles approaches infinity, it will almost surely result in a correctly sorted state of the array.
-
bogoSort
modifies the original array.
"Observing a quantum in a superposition causes the collapse of that state, making the quantum exhibit only particle-like properties. This theory suggests that each observation splits the universe into countless parallel universes, each with a definite state."
function quantumSort<T>(array: T[], compareFunc?: CompareFunc<T>): T[];
- Quantum-randomize the given array, this process divides the universe into multiple universes, each containing one possible permutation of the array;
- Observe each universe, if the array within is not sorted, destroy that universe;
- The remaining universes contain only sorted arrays.
-
quantumSort
may or may not modify the original array. - Unfortunately,
quantumSort
is not implemented, and will only throw anError
.
"Each number sleeps, and when they awaken at their own time, the world shall know their order."
function sleepSort(array: number[]): Promise<number[]>;
Each number in the array is assigned a delay time proportional to its value, allowing smaller numbers to "wake up" earlier and be added to the result array first. As all the numbers awaken one by one, a sorted array in ascending order emerges, as if they chose the correct order on their own.
-
sleepSort
doesn't modify the original array. -
sleepSort
only supports arrays ofnumber
type, and returns aPromise
.
"In space environments, there are large amounts of high-energy charged particles. CMOS electronic components in computers can be affected by Earth's magnetic field, cosmic rays, and other radiation, causing bit flips, turning 0 into 1, or 1 into 0."
function miracleSort<T>(array: T[], compareFunc?: CompareFunc<T>): T[];
Keep checking the arrayโs state... and wait for a miracle to happen!
-
miracleSort
modifies the original array.
"To exist is to be justified โ the arrayโs order is beyond question."
function divineSort<T>(array: T[], compareFunc?: CompareFunc<T>): T[];
Its very existence proves its correctness. The array has already been sorted in the best possible way, transcending our mortal understanding of what 'sorting' means. The array has achieved salvation.
-
divineSort
doesn't modify the original array. -
divineSort
doesn't support a customcompareFunc
โ if provided, it will throw anError
, because the divine will is not subject to mortal interference.
function quickSort<T>(array: T[], compareFunc?: CompareFunc<T>): T[];
Yes, this is a serious and legitimate implementation of the quicksort algorithm, used for benchmarking.
-
quickSort
modifies the original array.
This project was inspired by ไบบ็ฑปๅฅ่ฉๆๅบ็ฎๆณ้ด่ต - ๅๅฉๅๅฉ.
- โ๏ผFor questions or bug feedback, you can submit an issues and PR are welcome
- ๐ซ๏ผxiaohe0601@outlook.com
- ๐ง๏ผNot yet available
- MIT LICENSE