Sorting Algorithm Complexities | Java Fresher Interview
Fast recall
Quick revision
Core answer
Arrays.sort uses dual-pivot quicksort (primitives) and TimSort (objects).Whether you know the standard complexities, including quicksort's worst case and the O(n log n) lower bound
Saying quicksort is always O(n log n), or forgetting which sorts are stable / in-place
Note the O(n log n) comparison lower bound and that counting sort beats it by not comparing
Interview-ready guide
Interview answer
I'd group them.
The simple comparison sorts, bubble, insertion, and selection, are all O of n squared on average, so they're only practical for tiny inputs or nearly-sorted data, where insertion sort in particular is close to linear. The efficient comparison sorts are O of n log n. Merge sort is O of n log n guaranteed, it's stable, but it needs O of n extra space.
Heap sort is also O of n log n guaranteed and it's in place, but it's not stable. Quicksort is the interesting one: it's O of n log n on average and in place, but its worst case is O of n squared, which happens with bad pivot choices, classically on already-sorted input with a naive pivot.
A key fact I'd state is that no comparison-based sort can do better than O of n log n in the worst case; that's a proven lower bound, because comparisons can only extract so much information.
Non-comparison sorts like counting sort and radix sort get around that by using the values themselves as indices rather than comparing, so they can run in O of n plus k, where k is the range of keys, but they only help when that range is small.
Finally, I'd mention what Java actually uses, because it shows practical knowledge: Arrays dot sort uses a dual-pivot quicksort for primitive arrays, and TimSort, which is a hybrid of merge and insertion sort, for object arrays. The reason for the split is stability, primitives don't need it, but objects sorted by a key do.
Sorting Algorithm Complexities | Java Fresher Interview
Direct Answer
Comparison sorts: bubble, insertion, selection are O(n^2); merge sort is O(n log n) guaranteed; quicksort is O(n log n) average but O(n^2) worst; heap sort is O(n log n) guaranteed. Non-comparison sorts like counting/radix can hit O(n+k). The comparison-sort lower bound is O(n log n). Java's Arrays.sort uses dual-pivot quicksort (primitives) and TimSort (objects).
Code Example
int[] prims = {5, 2, 8, 1};
Arrays.sort(prims); // dual-pivot QUICKSORT for primitives
Integer[] objs = {5, 2, 8, 1};
Arrays.sort(objs); // TIMSORT (stable) for objects
// Stability matters when sorting objects by a key:
record P(String name, int age) {}
P[] people = { new P("Ann", 30), new P("Bob", 30) };
Arrays.sort(people, Comparator.comparingInt(P::age)); // Ann stays before Bob
Primitives don't need stability (equal ints are indistinguishable), so quicksort is fine; objects use stable TimSort.
Common Mistakes & Pitfalls
- Quicksort always O(n log n): its worst case is O(n^2) (bad pivots / sorted input).
- Merge sort is in-place: it needs O(n) auxiliary space.
- Ignoring stability: selection/quick/heap are not stable; merge and insertion are.
- Forgetting non-comparison sorts: counting/radix beat O(n log n) for small key ranges.
Check your understanding
Answer these without looking back at the article.
- How does merge sort work?
- Why is quicksort faster than merge sort in practice despite same Big-O?
- What is a stable sort?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.