# Least Significant Digit (LSD) Radix Sort in Java

Radix sort is a O(digits·keys) sorting algorithm which relies on integer key grouping to successfully process and naturally order the specified data set. Basing the sort on structure and positional notation, many other data types which can be represented in integer form (i.e. ASCII characters) can take advantage of the algorithm. Sorting occurs by acting on the comparison between item digits in the same position. Two alternative version of the algorithm exists, both tackling the problem from the opposite direction. In this post I will be describing an iterative lowest significant digit implementation which as the name hints at, starts processing from the right most digit position. This implementation results in a stable sort, where as the other implementation, tackles the most significant digit first and can not make such guarantees. In a stable sorting algorithm the initial ordering of equal keys is left unchanged in the result.

Above shows an example of a queue-based least significant digit radix sorting implementation. Starting from the right-most digit, the process occurs over multiple passes, distributing each item into calculated buckets, based on positional key. After each pass through the collection the items are retrieved in order from each bucket. This process is repeated up and including to the length of the longest key.

To make sure that the resulting processed data set is correctly sorted an assertion was included. Using such a feature is great in development, allowing you to verify the correctness of a specific invariant. This assertion can be activated at run-time by inclusion of the ‘-ea’ option in the ‘java’ command.