magic-sort
TypeScript icon, indicating that this package has built-in type declarations

0.0.2ย โ€ขย Publicย โ€ขย Published
logo

Magic Sort

๐Ÿค“ A fast and magical sort library, powered by luck

github stars npm version npm downloads bundle JSDocs License

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!

๐ŸŽ‰ Features

  • ๐Ÿช„ A variety of magical sorting algorithms
  • ๐ŸŽˆ Friendly API design
  • ๐Ÿง€ Full TypeScript support
  • ๐Ÿงช Complete unit testing
  • ๐Ÿ‘Œ No framework restrictions

๐Ÿš Installation

# pnpm
pnpm add magic-sort

# yarn
yarn add magic-sort

# npm
npm install magic-sort

๐Ÿ›น Usage

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]

๐Ÿ“– API

bogoSort

"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.

quantumSort

"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[];
  1. Quantum-randomize the given array, this process divides the universe into multiple universes, each containing one possible permutation of the array;
  2. Observe each universe, if the array within is not sorted, destroy that universe;
  3. 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 an Error.

sleepSort

"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 of number type, and returns a Promise.

miracleSort

"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.

divineSort

"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 custom compareFunc โ€” if provided, it will throw an Error, because the divine will is not subject to mortal interference.

quickSort

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.

๐Ÿฌ Thanks

This project was inspired by ไบบ็ฑปๅฅ‡่‘ฉๆŽ’ๅบ็ฎ—ๆณ•้‰ด่ต - ๅ“”ๅ“ฉๅ“”ๅ“ฉ.

๐Ÿถ Discussion & Communication

  • โ“๏ผšFor questions or bug feedback, you can submit an issues and PR are welcome
  • ๐Ÿ“ซ๏ผšxiaohe0601@outlook.com
  • ๐Ÿง๏ผšNot yet available

๐Ÿ† License

Readme

Keywords

none

Package Sidebar

Install

npm i magic-sort

Weekly Downloads

9

Version

0.0.2

License

MIT

Unpacked Size

21.6 kB

Total Files

8

Last publish

Collaborators

  • xiaohe0601