Knuth shuffle
npm install @algorithm.ts/knuth-shuffle
@algorithm.ts/knuth-shuffle
alt="Npm Version"
src="https://img.shields.io/npm/v/@algorithm.ts/knuth-shuffle.svg"
/>
alt="Npm Download"
src="https://img.shields.io/npm/dm/@algorithm.ts/knuth-shuffle.svg"
/>
alt="Npm License"
src="https://img.shields.io/npm/l/@algorithm.ts/knuth-shuffle.svg"
/>
alt="Module Formats: cjs, esm"
src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
/>
alt="Node.js Version"
src="https://img.shields.io/node/v/@algorithm.ts/knuth-shuffle"
/>
alt="Tested with Jest"
src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
/>
alt="Code Style: prettier"
src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
/>
A typescript implementation of the Knuth-Shuffle algorithm.
Knuth-Shuffle is a shuffle algorithm, which can complete the shuffle in $O(N)$
time complexity on the basis of only using a constant level of extra space.
If you are curious about this algorithm, you can visit [here][knuth-shuffle] for more details.
* npm
``bash`
npm install --save @algorithm.ts/knuth-shuffle
* yarn
`bash`
yarn add @algorithm.ts/knuth-shuffle
* deno
`typescript`
import knuthShuffle from 'https://raw.githubusercontent.com/guanghechen/algorithm.ts/main/packages/knuth-shuffle/src/index.ts'
* Shuffle nums.
`typescript
import knuthShuffle from '@algorithm.ts/knuth-shuffle'
knuthShuffle([1, 2, 3, 4, 5])
`
* Shuffle complex data nodes.
`typescript
import knuthShuffle from '@algorithm.ts/knuth-shuffle'
interface Node {
name: string
email: string
age: number
}
const nodes: Node[] = [
{ name: 'alice', email: 'alice@gmail.com', age: 40 },
/... omit .../
{ name: 'bob', email: 'lob@gmail.com', age: 40 },
]
knuthShuffle(nodes)
`
* Shuffle a contiguous range of the original array.
`typescript
import knuthShuffle from '@algorithm.ts/knuth-shuffle'
// shuffle { a[2], a[3], a[4], a[5], a[6] }
knuthShuffle([1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 7)
``
* [洗牌问题和 knuth-shuffle 算法][knuth-shuffle]
[homepage]: https://github.com/guanghechen/algorithm.ts/tree/release-2.x.x/packages/knuth-shuffle#readme
[knuth-shuffle]: https://me.guanghechen.com/post/algorithm/shuffle/#heading-knuth-shuffle