Initial Thoughts

Normally I would display more than two approaches, but shuffling is deceptively easy to do almost properly, and the Fisher-Yates algorithm is both the canonical solution and asymptotically optimal.

A few notes on randomness are necessary before beginning - both approaches displayed below assume that the languages' pseudorandom number generators (PRNGs) are sufficiently random. The sample code uses the simplest techniques available for getting pseudorandom numbers, but for each possible permutation of the array to be truly equally likely, more care must be taken. For example, an array of length has distinct permutations. Therefore, in order to encode all permutations in an integer space, bits are necessary, which may not be guaranteed by the default PRNG.

Approach #1 Brute Force [Accepted]

Intuition

If we put each number in a "hat" and draw them out at random, the order in which we draw them will define a random ordering.

Algorithm

The brute force algorithm essentially puts each number in the aforementioned "hat", and draws them at random (without replacement) until there are none left. Mechanically, this is performed by copying the contents of array into a second auxiliary array named aux before overwriting each element of array with a randomly selected one from aux. After selecting each random element, it is removed from aux to prevent duplicate draws. The implementation of reset is simple, as we just store the original state of nums on construction.

The correctness of the algorithm follows from the fact that an element (without loss of generality) is equally likely to be selected during all iterations of the for loop. To prove this, observe that the probability of a particular element being chosen on the th iteration (indexed from 0) is simply being chosen during the th iteration not being chosen before the th iteration. Given that the array to be shuffled has elements, this probability is more concretely stated as the following:

When expanded (and rearranged), it looks like this (for sufficiently large ):

For the base case (), it is trivial to see that . For , the numerator of each fraction can be cancelled with the denominator of the next, leaving the from the 0th draw as the only uncancelled denominator. Therefore, no matter on which draw an element is drawn, it is drawn with a chance, so each array permutation is equally likely to arise.

Complexity Analysis

  • Time complexity :

    The quadratic time complexity arises from the calls to list.remove (or list.pop), which run in linear time. linear list removals occur, which results in a fairly easy quadratic analysis.

  • Space complexity :

    Because the problem also asks us to implement reset, we must use linear additional space to store the original array. Otherwise, it would be lost upon the first call to shuffle.


Approach #2 Fisher-Yates Algorithm [Accepted]

Intuition

We can cut down the time and space complexities of shuffle with a bit of cleverness - namely, by swapping elements around within the array itself, we can avoid the linear space cost of the auxiliary array and the linear time cost of list modification.

Algorithm

The Fisher-Yates algorithm is remarkably similar to the brute force solution. On each iteration of the algorithm, we generate a random integer between the current index and the last index of the array. Then, we swap the elements at the current index and the chosen index - this simulates drawing (and removing) the element from the hat, as the next range from which we select a random index will not include the most recently processed one. One small, yet important detail is that it is possible to swap an element with itself - otherwise, some array permutations would be more likely than others. To see this illustrated more clearly, consider the animation below:

!?!../Documents/384_Shuffle_an_Array.json:697,161!?!

Complexity Analysis

  • Time complexity :

    The Fisher-Yates algorithm runs in linear time, as generating a random index and swapping two values can be done in constant time.

  • Space complexity :

    Although we managed to avoid using linear space on the auxiliary array from the brute force approach, we still need it for reset, so we're stuck with linear space complexity.


Analysis written by: @emptyset