I'd say, that foxcub's answer is wrong. To prove that I will introduce a helpful definition for a perfectly shuffled list (call it array or sequence or whatever you want).
Definition: Assume we have a List L
containing the elements a1, a2 ... an
and the indexes 1, 2, 3..... n
. If we expose the L
to a shuffle operation (to which internals we have no access) L
is perfectly shuffled if and only if by knowing indexes of some k (k< n
) elements we can't deduce the indexes of remaining n-k
elements. That is the remaining n-k
elements are equally probable to be revealed at any of the remaining n-k
indexes.
Example: if we have a four element list [a, b, c, d]
and after shuffling it, we know that its first element is a
([a, .., .., ..]
) than the probability for any of the elements b, c, d
to occur in, let's say, the third cell equals 1/3
.
Now, the smallest list for which the algorithm does not fulfil the definition has three elements. But the algorithm converts it to a 4-element list anyway, so we will try to show its incorrectness for a 4-element list.
Consider an input L = [a, b, c, d]
Following the first run of the algorithm the L will be divided into l1 = [a, c]
and l2 = [b, d]
. After shuffling these two sublists (but before merging into the four-element result) we can get four equally probable 2-elements lists:
l1shuffled = [a , c] l2shuffled = [b , d]
l1shuffled = [a , c] l2shuffled = [d , b]
l1shuffled = [c , a] l2shuffled = [b , d]
l1shuffled = [c , a] l2shuffled = [d , b]
Now try to answer two questions.
1. What is the probability that after merging into the final result a
will be the first element of the list.
Simply enough, we can see that only two of the four pairs above (again, equally probable) can give such a result (p1 = 1/2
). For each of these pairs heads
must be drawed during first flipping in the merge routine (p2 = 1/2
). Thus the probability for having a
as the first element of the Lshuffled
is p = p1*p2 = 1/4
, which is correct.
2. Knowing that a
is on the first position of the Lshuffled
, what is the probability of having c
(we could as well choose b
or d
without loss of generality) on the second position of the Lshuffled
Now, according to the above definition of a perfectly shuffled list, the answer should be 1/3
, since there are three numbers to put in the three remaining cells in the list
Let's see if the algorithm assures that.
After choosing 1
as the first element of the Lshuffled
we would now have either:
l1shuffled = [c] l2shuffled = [b, d]
or:
l1shuffled = [c] l2shuffled = [d, b]
The probability of choosing 3
in both cases is equal to the probability of flipping heads
(p3 = 1/2
), thus the probability of having 3
as the second element of Lshuffled
, when knowing that the first element element of Lshuffled
is 1
equals 1/2
. 1/2 != 1/3
which ends the proof of the incorrectness of the algorithm.
The interesting part is that the algorithm fullfils the necessary (but not sufficient) condition for a perfect shuffle, namely:
Given a list of n
elements, for every index k
(<n
), for every element ak
: after shuffling the list m
times, if we have counted the times when ak
occured on the k
index, this count will tend to m/n
by probability, with m
tending to infinity.