Shuffling is the act of randomizing the order of the elements in a collection. Sometimes the term shuffling is also used for reordering elements in a controllable way, e.g. the bytes within a vector, especially by respective instructions for SIMD architectures.
Shuffling is the process by which the order of the elements in a collection is randomized. The term shuffling is often used for playing cards.
One effective algorithm for doing so is Fischer-Yates Shuffle:
For a zero-based collection of n
elements:
Let
i = n
Let
j
= random integer between0
andi
(inclusive).Swap the values of the elements at
j
andi
i = i - 1
if (i > 0)
, go to Step-2
The algorithm has a time complexity of O(n)
.