Quick revision
The short answer
Arrays.sort(int[]) and the other primitive overloads use a dual-pivot quicksort, which sorts in place, uses insertion sort for tiny ranges and, in recent JDKs, falls back to heap sort or merging to avoid quadratic worst cases; it is not stable, which does not matter because equal primitives are indistinguishable. Arrays.sort(Object[]) and Collections.sort use TimSort, a stable, adaptive merge sort that is O(n log n) in the worst case, close to O(n) on partly sorted input, and needs up to n/2 extra references. Objects need stability because equal keys can still be different objects, and Collections.sort simply delegates to List.sort, which for ArrayList sorts its backing array directly.
What to remember
In this answer, we saw that the JDK sorts primitive arrays with a dual-pivot quicksort that is fast, in place and protected against quadratic cases by insertion sort, run merging and a heap sort fallback, while objects are sorted with TimSort, a stable, adaptive merge sort with an O(n log n) worst case that needs up to n/2 extra references. Stability is irrelevant for indistinguishable primitives but essential for objects, for example to keep first-name order after sorting by last name. Collections.sort delegates to List.sort, which ArrayList implements by sorting its backing array directly.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Arrays.sort(int[]) and the other primitive overloads use a dual-pivot quicksort, which sorts in place, uses insertion sort for tiny ranges and, in recent JDKs, falls back to heap sort or merging to avoid quadratic worst cases; it is not stable, which does not matter because equal primitives are indistinguishable. Arrays.sort(Object[]) and Collections.sort use TimSort, a stable, adaptive merge sort that is O(n log n) in the worst case, close to O(n) on partly sorted input, and needs up to n/2 extra references. Objects need stability because equal keys can still be different objects, and Collections.sort simply delegates to List.sort, which for ArrayList sorts its backing array directly.
Why this question is asked
Sorting is so common that we rarely think about which algorithm runs underneath, yet the choice explains behaviour we see in real code: why sorting objects allocates memory, why a sorted list of employees keeps a previous ordering among equal salaries, and why a buggy comparator throws an exception on some data but not on other data. Interviewers ask this to check that we know the difference between stable and unstable sorts and why the JDK picks different algorithms for different element types. In this answer, we will recap what stability means, look at the primitive and object algorithms in turn, see a concrete example of why objects need stability, and finish with how Collections.sort and List.sort fit together.
1A Quick Recap of Stability
A sort is stable if elements that compare as equal keep the order they had before sorting. Suppose we sort people by age, and Asha and Ben are both 30, with Asha first in the input. A stable sort guarantees Asha stays before Ben. An unstable sort may swap them.
For primitives, stability is meaningless. Two ints with the value 30 are indistinguishable, so nobody can tell whether they were swapped. For objects, it matters a lot: two Person objects with the same age are still different people, with different names and ids, so their relative order is visible and often important.
| Call | Algorithm | Stable | Extra memory |
|---|---|---|---|
Arrays.sort(int[]), long[], double[] ... | dual-pivot quicksort with fallbacks | no | in place (small stack) |
Arrays.sort(Object[]), Arrays.sort(T[], cmp) | TimSort | yes | up to n/2 references |
Collections.sort(list), list.sort(cmp) | TimSort via List.sort | yes | up to n/2, plus a copy for some lists |
Arrays.parallelSort(...) | parallel merge of sorted chunks | objects: yes | extra working buffer |
2Primitives: Dual-Pivot Quicksort
Since Java 7, the primitive overloads use the dual-pivot quicksort developed by Vladimir Yaroslavskiy, with Jon Bentley and Joshua Bloch. Classic quicksort picks one pivot and splits the array into two parts, smaller and larger. Dual-pivot quicksort picks two pivots, p1 <= p2, and splits the array into three parts: less than p1, between the pivots, and greater than p2. In practice that means fewer passes over memory and better use of CPU caches, so it is measurably faster than single-pivot quicksort on modern hardware.
Quicksort has a well-known weakness: with unlucky pivots, it degrades to O(n squared). The JDK's implementation guards against that. For tiny ranges, a few dozen elements, it switches to insertion sort, which is fastest at that size. In recent JDKs, since a rewrite in Java 14, it also detects input that is already made of sorted runs and merges them, and if recursion gets too deep, it falls back to heap sort, which guarantees O(n log n). It sorts in place, using only a small amount of stack, which suits large primitive arrays well.
Notice that being unstable costs nothing here, while sorting in place saves memory, which for an array of 100 million ints is 400 MB we do not have to allocate again.
3Objects: TimSort
For objects, the JDK uses TimSort, which Tim Peters designed for Python and which Java adopted in Java 7. TimSort is a merge sort that takes advantage of order already present in the data. It scans the input for runs, stretches that are already ascending or strictly descending (descending runs are reversed in place), extends short runs to a minimum length with binary insertion sort, and then merges runs in a carefully balanced way. Arrays of fewer than 32 elements are simply sorted with binary insertion sort.
This gives it useful properties. It is stable, because merging never moves an element past an equal one from a later run. Its worst case is O(n log n), with no quadratic case. And it is adaptive: on input that is already sorted or nearly sorted, which is very common in real data, it does close to n comparisons, so it runs in close to O(n). The cost is memory: merging needs a temporary array, up to n/2 references in the worst case, whereas quicksort sorts in place.
4Why Stability Matters for Objects
Stability lets us sort by several keys in separate passes, and it means a sort never scrambles an order the user already sees. Here is the classic example.
Because TimSort is stable, after the second sort the two Raos are still in first-name order, and so are the two Shahs. An unstable sort could have produced Ravi Rao before Asha Rao. The same property is what keeps a table in a user interface from reshuffling rows with equal values every time the user re-sorts by another column.
5How Collections.sort and List.sort Fit Together
Collections.sort(list) is older than List.sort, and since Java 8 it simply calls list.sort(null) or list.sort(comparator). The default implementation of List.sort copies the elements into an array, sorts the array with Arrays.sort, which means TimSort, and then writes the elements back through a ListIterator. That works for any list, including LinkedList, which could not be sorted efficiently in place.
ArrayList overrides sort() to skip the copy: it sorts its own backing array directly with Arrays.sort and then increments its modification count, so an iterator that was open during the sort fails fast with ConcurrentModificationException. Immutable lists, such as the ones from List.of(), throw UnsupportedOperationException when sorted, so we copy them into a new ArrayList first, as in the example above.
6Follow-up questions
What does 'Comparison method violates its general contract' mean?
TimSort assumes the comparator is consistent: compare(a, b) must have the opposite sign of compare(b, a), and the order must be transitive. When its merge step finds evidence that this is not true, it throws IllegalArgumentException with that message. It is a bug in the comparator, often one that never returns zero or mishandles NaN, and it appears only for some inputs because the check happens only in certain merge situations.
When would you use Arrays.parallelSort?
For large arrays, typically from hundreds of thousands of elements upwards, when spare CPU cores are available. It splits the array into chunks, sorts them in parallel on the common ForkJoinPool and merges them; below a threshold it just sorts sequentially. In a busy web service, using all cores for one request's sort can hurt other requests, so it suits batch jobs better than request handling. Measure before adopting it.
Why not use TimSort for primitives too?
Because stability buys nothing for primitives, while TimSort's extra memory and its object-oriented merging overhead would cost something. Dual-pivot quicksort with its fallbacks is faster on typical primitive data and sorts in place. The JDK chooses the best algorithm for each case rather than one algorithm for all.
7Conclusion
In this answer, we saw that the JDK sorts primitive arrays with a dual-pivot quicksort that is fast, in place and protected against quadratic cases by insertion sort, run merging and a heap sort fallback, while objects are sorted with TimSort, a stable, adaptive merge sort with an O(n log n) worst case that needs up to n/2 extra references. Stability is irrelevant for indistinguishable primitives but essential for objects, for example to keep first-name order after sorting by last name. Collections.sort delegates to List.sort, which ArrayList implements by sorting its backing array directly.
Open with
For primitive arrays, Arrays.sort uses dual-pivot quicksort. It picks two pivots and splits the range into three parts, which is faster than classic quicksort on modern CPUs, and it sorts in place.
Then, if they want more
It uses insertion sort for very small ranges, and in recent JDKs it also merges already sorted runs and falls back to heap sort if recursion gets too deep, so it avoids the quadratic worst case. It isn't stable, but for primitives that doesn't matter, because two equal ints are indistinguishable.
For object arrays and for Collections.sort, the JDK uses TimSort, a stable merge sort that finds existing runs in the data and merges them. Its worst case is O(n log n), and on nearly sorted data, which is common, it's close to linear. The trade-off is memory: it needs up to n over two extra references for merging.
Stability matters for objects because equal keys can still be different objects. If I sort people by first name and then by last name, a stable sort keeps the first-name order among people with the same last name, and a table in a UI doesn't reshuffle equal rows. Collections.sort just delegates to List.sort.
The default implementation copies to an array, sorts it and writes back, while ArrayList overrides it to sort its backing array directly. Immutable lists throw UnsupportedOperationException, so I copy them first.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 7, Java 14, Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Which algorithms does the JDK use for Arrays.sort(int[]), Arrays.sort(Object[]) and...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/5e1d4293cc/