LOGBOOK

HELP

Quiz Entry - updated: 2026.12.02

Why does Java sort an int[] with a quick-sort, but an array of objects with a merge-sort variant?

For primitive values two equal numbers are indistinguishable, so stability is irrelevant and the fast in-place dual-pivot quick-sort is used. Objects can compare as equal and still be different records, so Java guarantees a stable sort for them and uses TimSort, a merge-sort hybrid.

What the Java API documentation promises:

Call Algorithm Stable
Arrays.sort(int[]) and the other primitive types dual-pivot quick-sort (two pivots, three parts) no need: equal values are identical
Arrays.sort(Object[]), Collections.sort, List.sort TimSort, an adaptive merge-sort guaranteed

TimSort was written by Tim Peters for Python's list.sort() and later adopted by Java. It looks for runs that are already sorted in the input, extends short runs with insertion-sort, and merges the runs, merge-sort style. On input that is already sorted it finishes in $O(n)$, otherwise it needs $O(n \log n)$, and it is stable because its merges prefer the left run on ties. Python's sorted() and list.sort() are stable for the same reason.

Go deeper:

From Quiz: ADS / Merge-Sort and Quick-Sort | Updated: Dec 02, 2026