American flag sort

American flag sort is most efficient with a radix that is a power of 2, because bit-shifting operations can be used instead of expensive exponentiations to compute the value of each digit.

Depending on the hardware, it may be worth clearing the counts in correspondence with completing a bucket (as in the original paper); or it may be worth maintaining a max and min active bucket, or a more complex data structure suitable for sparse arrays.

It is also important to use a more basic sorting method for very small data sets, except in pathological cases where keys may share very long prefixes.

This example written in the Python programming language will perform American flag sort for any radix of 2 or greater.

Simplicity of exposition is chosen over clever programming, and so the log function is used instead of bit shifting techniques.