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. One effective algorithm for doing so is as follows:
For a zero-based collection of
- i = n
- j = random integer between 0 and i (inclusive)
- Swap the values of the elements at j and i
- i = i - 1
- If (i > 0), go to 2
This is called the Fisher-Yates shuffle and has a time complexity of O(n).