@algorithm.ts/shuffle
TypeScript icon, indicating that this package has built-in type declarations

4.0.3 • Public • Published

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 for more details.

Install

  • npm

    npm install --save @algorithm.ts/shuffle
  • yarn

    yarn add @algorithm.ts/shuffle

Usage

  • Shuffle nums.

    import { knuthShuffle } from '@algorithm.ts/shuffle'
    
    knuthShuffle([1, 2, 3, 4, 5])
  • Shuffle complex data nodes.

    import { knuthShuffle } from '@algorithm.ts/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 the elements which indexes in the customized contiguous range.

    import { knuthShuffle } from '@algorithm.ts/shuffle'
    
    // shuffle { a[2], a[3], a[4], a[5], a[6] }
    knuthShuffle([1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 7)

Related

Package Sidebar

Install

npm i @algorithm.ts/shuffle

Weekly Downloads

72

Version

4.0.3

License

MIT

Unpacked Size

11.3 kB

Total Files

7

Last publish

Collaborators

  • lemonclown