Shuffle
Bill Baxter
dnewsgroup at billbaxter.com
Fri Jan 25 14:52:06 PST 2008
BCS wrote:
> Oskar Linde wrote:
>> Walter Bright wrote:
>>
>>> /* Shuffle the files[] array
>>> */
>>
>>
>> To be nitpicky, your algorithm will not give a perfect shuffling.
>> Consider shuffling the sequence 012. The algorithm has 3^3 = 27
>> different ways it can shuffle the sequence, and there exists 3! = 6
>> different permutations of the sequence. Since 27 isn't divisible by 6,
>> the algorithm will give a slight bias towards some end sequences.
>> (021,102 and 120 in this case). A quick fix is to change the first
>> line of the loop body:
>>
>
> What would you get if you qsorted the list with rand used as the compare
> function?
At best, an O(N log N) shuffle instead of O(N).
--bb
More information about the Digitalmars-d-announce
mailing list