Java Sorting – Why Not Use Radix Sort on Primitives?

javasorting

java.util.Arrays.sort(/* int[], char[], short[], byte[], boolean[] */) is implemented as a 'tuned quicksort' rather than a radix sort.

I did a speed comparison a while ago, and with something like n>10000, radix sort was always faster. why?

Best Answer

I would speculate that:

  • Array.sort is implemented as quicksort, because quicksort can sort anything in decent time given a comparator.
  • Sorting a list of 10000 entries is not so common. Accessing a data-structure of 10000 or more elements is rather common. If you need to maintain order, a balanced search tree is often a better way to go than to sort your whole array every time you need the smallest element.
  • Sorting primitives is not so common, despite what university may teach.

The point is, it's not such a common use case, that it's optimization needs to be in the standard library. If you have written an application, that has performance issues, where you determine through profiling that sorting an array of 10000+ ints is actually the bottleneck, then you might as well write the sorting by hand or reconsider your choice of data structure in the first place.