@algoritm.ts/shuffle (英语)
类型标记执行 Knuth- 挥发 算法.
Knuth-Shuffle是一个洗牌算法,它可以在 $O(N) 时间复杂度中完成洗牌 仅使用常数额外空间的基础.
如果你对这个算法好奇,你可以去拜访 这里 更多细节.
安装
-
无
npm install --save @algorithm.ts/shuffle -
小时
pnpm add @algorithm.ts/shuffle
使用量
-
花花肠.
import { knuthShuffle } from '@algorithm.ts/shuffle' knuthShuffle([1, 2, 3, 4, 5]) -
Shuffle 复杂数据节点.
import { knuthShuffle } from '@algorithm.ts/shuffle' interface Node { name: string email: string age: number } const nodes: Node[] = [ { name: 'alice', email: '[email protected]', age: 40 }, /*... omit ...*/ { name: 'bob', email: '[email protected]', age: 40 }, ] knuthShuffle(nodes) -
在定制的相接区域中移动索引的元素.
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)