Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数组乱序(时间复杂度O(n)) #508

Open
Sunny-117 opened this issue Apr 16, 2024 · 1 comment
Open

数组乱序(时间复杂度O(n)) #508

Sunny-117 opened this issue Apr 16, 2024 · 1 comment

Comments

@Sunny-117
Copy link
Owner

No description provided.

@Lupengcheng7
Copy link

function shuffle(array){
    for(let i = array.length - 1; i>=0; i--){
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }

    return array
}

// 测试
const arr = [1, 2, 3, 4, 5];
const shuffledArr = shuffle(arr);
console.log(shuffledArr);

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

No branches or pull requests

2 participants