4.0.3 • Published 8 months ago

@algorithm.ts/shuffle v4.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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

4.0.3

8 months ago

4.0.2

9 months ago

4.0.1

1 year ago

4.0.0

1 year ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0-alpha.8

2 years ago

3.0.0

2 years ago

3.0.0-alpha.3

2 years ago

3.0.0-alpha.2

2 years ago

3.0.0-alpha.5

2 years ago

3.0.0-alpha.4

2 years ago

3.0.0-alpha.7

2 years ago

3.0.0-alpha.6

2 years ago

3.0.0-alpha.1

3 years ago

3.0.0-alpha.0

3 years ago