Java Arrays.sort Internals | Fresher Interview
Fast recall
Quick revision
Core answer
Arrays.sort uses two different algorithms: a dual-pivot quicksort for primitive arrays (int[], double[], ...) and TimSort - a hybrid of merge sort and insertion sort - for object arrays and Collections.sort. The split exists because objects need a stable sort and primitives don't (equal primitives are indistinguishable), and TimSort exploits already-ordered 'runs' in real-world data.Whether you know the primitive-vs-object split and why stability drives it
Saying Java uses 'merge sort' or 'quicksort' for everything, missing the dual approach
Explain that primitives skip stability (so quicksort is fine) while objects use stable TimSort
Interview-ready guide
Interview answer
The thing most people get wrong is assuming Arrays dot sort uses a single algorithm.
It actually uses two, depending on what you're sorting. For primitive arrays, like int arrays or double arrays, it uses a dual-pivot quicksort. For object arrays, and also for Collections dot sort and List dot sort, it uses TimSort, which is a hybrid of merge sort and insertion sort.
The reason for that split is stability. A stable sort keeps equal elements in their original relative order, and that matters a lot when you sort objects by a key, for example sorting employees by department while keeping the original ordering within each department. TimSort is stable, so it's the right choice for objects.
Quicksort is not stable, but for primitives that's completely fine, because two equal integers are genuinely identical, there's no hidden information to preserve, so order among equal primitives is meaningless. That lets Java use the faster, in-place quicksort for primitives without any downside.
The other nice property of TimSort is that it detects already-sorted runs in the data and merges them, so on real-world data that's partially ordered it runs close to linear time, while still guaranteeing O of n log n in the worst case. So my one-line summary is: dual-pivot quicksort for primitives because stability isn't needed, and stable, run-aware TimSort for objects because it usually is.
Java Arrays.sort Internals | Fresher Interview
Direct Answer
Arrays.sort uses two different algorithms: a dual-pivot quicksort for primitive arrays (int[], double[], ...) and TimSort - a hybrid of merge sort and insertion sort - for object arrays and Collections.sort. The split exists because objects need a stable sort and primitives don't (equal primitives are indistinguishable), and TimSort exploits already-ordered 'runs' in real-world data.
Code Example
// Primitives -> dual-pivot quicksort (not stable, but doesn't matter)
int[] nums = {5, 3, 8, 1};
Arrays.sort(nums); // [1, 3, 5, 8]
// Objects -> TimSort (stable)
record Emp(String name, int dept) {}
Emp[] emps = { new Emp("Ann", 2), new Emp("Bo", 1), new Emp("Cy", 2) };
Arrays.sort(emps, Comparator.comparingInt(Emp::dept));
// dept order preserved AND Ann stays before Cy (both dept 2) -> stable
List<Emp> list = new ArrayList<>(List.of(emps));
list.sort(Comparator.comparingInt(Emp::dept)); // also TimSort
Common Mistakes & Pitfalls
- 'Java uses merge sort': only for objects (TimSort); primitives use quicksort.
- 'Java uses quicksort': only for primitives; objects use stable TimSort.
- Assuming primitive sort is stable: it's not, but it doesn't matter for primitives.
- Ignoring TimSort's run detection: it's near-linear on partially sorted data.
Check your understanding
Answer these without looking back at the article.
- What does stable sort mean?
- How do you sort objects by multiple fields in Java?
- What is the difference between Comparable and Comparator?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.