<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:"Courier New";}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1><pre>The discussion of randomly permuting a list comes up every few years.&nbsp; Here&#8217;s <o:p></o:p></pre><pre>what I wrote last time (2005):<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>&#8220;Clearly, you can do a perfect shuffle in O(N) time using mutable arrays,<o:p></o:p></pre><pre>using the standard imperative algorithm.&nbsp; You can also do it in O(N)<o:p></o:p></pre><pre>expected time using *immutable* arrays, using Haskell's bulk array<o:p></o:p></pre><pre>operations.<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>1. Start with a list of length N. &nbsp;If N &lt; 2, stop.<o:p></o:p></pre><pre>2. Pair each element with a random index in the range [1,2N] (or<o:p></o:p></pre><pre>[0,2N-1]).<o:p></o:p></pre><pre>3. Use &quot;accum&quot; to create an array of size 2N, where slot I contains a<o:p></o:p></pre><pre>list of all the elements paired with I.<o:p></o:p></pre><pre>4. Map the shuffle function recursively across the array to re-shuffle<o:p></o:p></pre><pre>any slots that got more than one element.<o:p></o:p></pre><pre>5. Append all the slots together into the result list.<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>If you leave out step 4, you get an algorithm that runs in O(N)<o:p></o:p></pre><pre>worst-case time, rather than expected time, but you no longer have a<o:p></o:p></pre><pre>perfect shuffle (although it might be good enough).&nbsp; Upping the size of<o:p></o:p></pre><pre>the array from 2N to 3N or 4N will help reduce collisions, but will not<o:p></o:p></pre><pre>entirely eliminate them.<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>With step 4, you can imagine pathological cases where everything gets<o:p></o:p></pre><pre>put in the same slot, but with a reasonable random number generator, it<o:p></o:p></pre><pre>is vanishingly unlikely that you will have enough collisions to drive<o:p></o:p></pre><pre>the cost higher than linear.&nbsp; Again, upping the size of the array from<o:p></o:p></pre><pre>2N to 3N or 4N makes this even more unlikely.&#8221;<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>Now, you can consider it cheating to say this is O(N) expected time<o:p></o:p></pre><pre>because it uses O(N log N) random bits.&nbsp; Fair enough.&nbsp; It is O(N) to the<o:p></o:p></pre><pre>same extent that the ordinary imperative algorithm is O(N), albeit expected<o:p></o:p></pre><pre>rather than worst case.<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>-- Chris<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre></div>

</body>

</html>