title: Radix Sort in JavaScript
body: <p>I've come up with the following but it predictably doesn't work.</p>
  
  <pre><code>var t = new Array(a.length);
  var r = 4;
  var b = 64;
  
  var count = new Array(1&lt;&lt;r);
  var pref = new Array(1&lt;&lt;r);
  
  var groups = Math.ceil(b / r);
  
  var mask = (1 &lt;&lt; r) - 1;
  
  var shift = 0;
  for(var c = 0; c &lt; groups; c++)
  {
      shift += r;
  
      for(var j = 0; j &lt; count.length; j++)
      {
          count[j] = 0;
      }
  
      for(var i = 0; i &lt; a.length; i++)
      {
          count[ (a[i] &gt;&gt; shift) &amp; mask ]++;
      }
  
      pref[0] = 0;
  
      for(var i = 0; i &lt; count.length; i++)
      {
          pref[i] = pref[i-1] + count[i-1];
      }
  
      for(var i = 0; i &lt; a.length; i++)
      {
          t[ pref[ (a[i] &gt;&gt; shift) &amp; mask ]++ ] = a[i];
      }
  
      for(var i = 0; i &lt; a.length; i++)
      {
          a[i] = t[i];
      }
      // a is sorted?
  }
  </code></pre>