LOGBOOK

HELP

Quiz Entry - updated: 2026.11.27

Why is bucket-sort stable, and which implementation detail decides it?

A sort is stable if entries with equal keys keep their input order. Bucket-sort is stable because every bucket works as a queue: phase 1 appends at the end, phase 2 takes from the front, so equal keys leave in the order they arrived. Insert at the front of a bucket, or empty it from the back, and equal keys come out reversed.

In the example the entries with key 7 arrive as $(7,d), (7,g), (7,e)$ and leave in exactly that order. Buckets used as stacks would produce $(7,e), (7,g), (7,d)$: still sorted by key, but no longer stable.

Stability only matters when entries with equal keys can be told apart, which is the normal case for records. Picture a ticket shop that serves orders in the order they arrive but stamps each order with the date only, not the time. Re-sort the orders by date with an unstable sort, and a customer who ordered on Friday morning can end up behind one who ordered on Friday afternoon, and lose the last concert ticket. A stable sort keeps the arrival order within each day. Stability is also essential for radix-sort, which sorts by one digit after another and relies on each pass keeping the order that the previous passes built.

A rule of thumb for spotting unstable algorithms: they move elements in long jumps over other elements, as the swaps of heap-sort and in-place quick-sort do. Algorithms that only append in order or move elements past their direct neighbours, like bucket-sort, merge-sort and insertion-sort, can keep ties in order.

In code the queue behaviour comes from choosing the right operations: add and removeFirst() on a Java LinkedList, append and popleft() on a Python deque.

Go deeper:

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