LOGBOOK

HELP

Quiz Entry - updated: 2026.11.27

What must bucket-sort's keys be, and why does it need no comparator?

The keys must be integers in a known range, or something that can be turned into one, because each key is used directly as an array index. For the same reason no comparator is needed: the order comes from the order of the bucket indices, not from comparing keys with each other.

An array index has to be a small non-negative integer, so arbitrary objects such as Person records, or floating-point numbers, cannot go into B[k] as they are. A comparison sort only needs to know which of two keys comes first. Bucket-sort needs to know where each key belongs among all possible keys, a much stronger requirement.

Many real keys are such integers already: postal codes, personnel and article numbers, ages in years, or months numbered from January = 0 to December = 11. A tempting shortcut does not work: every Java object has a hashCode(), which is an integer, but a hash code is designed to scatter keys, not to keep their order (by default it does not even depend on the object's contents), so sorting by it produces no meaningful order. What counts is an integer that preserves the order you want, such as getAge() for sorting people by age.

What bucket-sort gets in return: no comparisons at all, so the $\Omega(n\log n)$ bound for comparison sorts does not apply, and no Comparator or comparison function to write, because "smaller" simply means "lower bucket index".

Go deeper:

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