Shuffling is the act of randomizing the order of the elements in a collection.
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)
.