LOGBOOK

HELP

Quiz Entry - updated: 2026.11.27

Where does Java's own Arrays.sort stop comparing and sort by counting instead, and why exactly there?

In JDK 21, Arrays.sort on a byte[] with more than 64 elements, and on a char[] or short[] with more than 1 750, uses counting sort. A byte has only 256 possible values and a char or short 65 536, so one counter per value is affordable, and $O(n + N)$ beats $O(n\log n)$ once there are enough elements. An int has $2^{32}$ values, far too many counters, so int[] gets dual-pivot quick-sort.

Counting sort for bytes takes two loops: count how often each of the 256 values occurs, then walk through the counters from the smallest value upwards and write each value into the array as often as it was counted. No element is ever compared with another. It is bucket-sort with $N = 256$, where each bucket shrinks to a counter, because equal bytes are indistinguishable and need no list.

The thresholds follow the $n + N$ logic. With only 256 counters, counting pays off from a few dozen elements on (up to 64 elements the library uses insertion-sort). With 65 536 counters for char and short, the counter array only pays off for much longer arrays, so the threshold is far higher.

Go deeper:

From Quiz: ADS / Sorting Lower Bound, Bucket-Sort and Radix-Sort | Updated: Nov 27, 2026