Skip to content

Conversation

@wyp20040208123
Copy link

static RandomSort(list) {
// 检查输入是否是数组
if (!Array.isArray(list)) {
throw new Error('Input must be an array');
}

// Fisher-Yates 洗牌算法
for (let i = list.length - 1; i > 0; i--) {
    // 随机选择一个索引 j,范围为 0 到 i
    const j = Math.floor(Math.random() * (i + 1));
    
    // 交换 list[i] 和 list[j]
    [list[i], list[j]] = [list[j], list[i]];
}

return list;  // 返回打乱后的数组

}

songwei and others added 5 commits February 28, 2019 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants