Skip to content

Latest commit

 

History

History
43 lines (28 loc) · 1.72 KB

README.md

File metadata and controls

43 lines (28 loc) · 1.72 KB

Fisher-Yates Shuffle

en pt-br

Fisher-Yates shuffle, also known as Knuth shuffle, is an algorithm for generating a random permutation of a finite sequence. It efficiently shuffles an array, making each possible permutation equally likely.

The algorithm works by dividing the input array into two parts: a shuffled section and an unshuffled section. It repeatedly takes a random element from the unshuffled section and inserts it into the shuffled section, swapping the elements as needed.

Algorithm Steps

  1. Start with the last element of the array.
  2. Swap the current element with a randomly selected element before it (including itself).
  3. Move to the previous element and repeat step 2.
  4. Continue this process until the first element of the array is reached.

Source code

Time Complexity

Case Complexity
Worst O(n)
Average O(n)
Best O(n)

Space Complexity

Case Complexity
Worst O(1)
Average O(1)
Best O(1)

Bias

The algorithm itself is unbiased, but the random number generator used to select the elements may not be. In this implementaion, since the Math.random() function from JavaScript is a pseudo-random number generator (PRNG), the algorithm isn't truly unbiased. However, the bias is so small that it can be ignored for most applications.

References

Return to main page