Skip to content

其他

Fisher-Yates Shuffle (Knuth Shuffle)

Random shuffle in \(O(N)\) time complexity.

void shuffle(vector<int>& nums) {
    for (int i = 0; i < nums.size(); i++) {
        int j = i + rand() % (nums.size() - i);
        swap(nums[i], nums[j]);
    }
}