Radix sort

Radix sort has developed from its physical counterpart, where records were stored on punch cards and these cards were distributed into buckets during a single pass of their stack through a mechanical card sorter. For details, see

The idea of radix sort is shown in a nice animation written by Woi Ang, where the keys are two-digit integers with radix 10.


We shall consider keys that are 32-bit nonnegative integers and we shall treat them as four-digit integers with radix 256: if

n = a31231+ a30230+ ... + a222+ a121+ a020
with each ai equal to 0 or 1, then
n = b32563+ b22562+ b12561+ b02560
with
b3= a3127+ a3026+ a2925+ a2824+ a2723+ a2622+ a2521+ a2420,
b2= a2327+ a2226+ a2125+ a2024+ a1923+ a1822+ a1721+ a1620,
b1= a1527+ a1426+ a1325+ a1224+ a1123+ a1022+ a921+ a820,
b0= a727+ a626+ a525+ a424+ a323+ a222+ a121+ a020.

Given any integer n in the interval [0,232 ), we can compute the corresponding b0 , b1 , b2 , b3 by the straight-line program

b0 = n mod 256;
n = (n-b0 )/256;
b1 = n mod 256;
n = (n-b1 )/256;
b2 = n mod 256;
n = (n-b2 )/256;
b3 = n;
In C, these four quantities can be computed very fast by two operators for bit manipulation, and Specifically,consider any machine that follows the convention of representing

0 as 00...000,
1 as 00...001,
2 as 00...010,
3 as 00...011,
... .....
231-1 as 01...111,
Given any integer n in the interval [0,231 ), we can compute the corresponding b0 , b1 , b2 , b3 by
b0 = n & 255
b1 = (n >> 8) & 255
b2 = (n >> 16) & 255
b3 = (n >> 24) & 255
Operators for bit manipulation are the subject of


Radix sorting linked lists

stack *rsort(stack *my_stack)
{
  int shift, i;
  stack *bucket[256];
  stack_object *first;

  for(shift=0; shift<32; shift+=8)
    {
      for(i=0; i<256; i++)
        bucket[i]=create_stack();
      while(!(stack_is_empty(my_stack)))
        {
          first=top_of_stack(my_stack);
          push_on_stack(first,bucket[((first->key)>>shift)&255]);
          pop_stack(my_stack);
        }

      for(i=255; i>=0; i--)
        while(!(stack_is_empty(bucket[i])))
          {
            first=top_of_stack(bucket[i]);
            push_on_stack(first,my_stack);
            pop_stack(bucket[i]);
          }
    }
  return my_stack;
}


Radix sorting arrays

void rsort(record a[], int n)
{
  int i,j;
  int shift;
  record temp[MAXLENGTH];
  int bucket_size[256], first_in_bucket[256];
 
  for(shift=0; shift<32; shift+=8)
    {
      /* compute the size of each bucket and
         copy each record from array a to array temp */
      for(i=0; i<256; i++)
        bucket_size[i]=0;
      for(j=0; j<n; j++)
        {
          i=(a[j].key>>shift)&255;
          bucket_size[i]++;
          temp[j]=a[j];
        }

      /* mark off the beginning of each bucket */
      first_in_bucket[0]=0;
      for(i=1; i<256; i++)
        first_in_bucket[i]=first_in_bucket[i-1]+bucket_size[i-1];

      /* copy each record from array temp to its bucket in array a */
      for(j=0; j<n; j++)
        {
          i=(temp[j].key>>shift)&255;
          a[first_in_bucket[i]]=temp[j];
          first_in_bucket[i]++;
        }
    }
}
    

Back to the table of contents of Vašek Chvátal's course notes