Sorting algorithm of Arrays in Java.util package
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
java.util.Arrays does not use one universal sorting algorithm for all array types. Primitive arrays and object arrays are handled differently, and parallel sorting has its own tradeoffs. Understanding these differences helps you choose the right method and avoid false assumptions about stability and performance.
Primitive Arrays and Arrays.sort
For primitive arrays, Arrays.sort uses an in-place dual-pivot quicksort in modern JDKs.
Important characteristics:
- In-place behavior.
- Average
O(n log n)complexity. - Not stable for equal primitive values, which usually matters only when external identity is tracked.
For basic numeric arrays, this is the standard default.
Object Arrays and Stable Sorting
Object-array sorting uses a stable algorithm with comparator support.
Stable sorting preserves relative order of equal keys, which is useful in multi-step ordering workflows.
Comparator Design Matters
In object sorting, comparator cost can dominate runtime. Keep comparators cheap and deterministic.
Good pattern:
Avoid expensive operations inside comparator calls, such as regex parsing or network lookups.
Arrays.parallelSort Tradeoffs
Arrays.parallelSort can improve performance on large arrays by using multiple threads.
For small arrays, overhead can outweigh benefits. Benchmark before adopting parallel sort globally.
Benchmarking Strategy
To compare sort methods fairly:
- Warm up the JVM.
- Use realistic data distributions.
- Run multiple iterations.
- Measure end-to-end use case, not only isolated sort call.
Single-shot timing often misleads because JIT and cache behavior dominate early runs.
Selecting the Right Method
Practical rules:
- Primitive arrays: start with
Arrays.sort. - Very large arrays: compare
sortandparallelSorton target hardware. - Object arrays with custom order:
Arrays.sortwith explicit comparator chain. - Multi-key ordering: use
thenComparingor stable multi-pass approach.
Correctness and comparator quality usually matter more than algorithm micro-details.
Stability and Multi-Key Workflows
When processing business records, stability affects correctness for chained sorts. For object arrays, stable sorting allows secondary-key sorts to be applied first and preserved by later primary-key sorts. For primitive arrays, stability is usually irrelevant because values carry no extra attached identity, but for boxed or custom object types it can change result interpretation in reporting pipelines.
Common Pitfalls
- Assuming all array sorts in
Arraysare stable. - Writing comparators that violate transitivity and break ordering.
- Using
parallelSortfor small arrays and slowing performance. - Ignoring comparator cost while tuning sort performance.
- Benchmarking without JVM warmup and drawing incorrect conclusions.
Summary
- '
Arrays.sortbehavior differs for primitives and objects.' - Primitive sorting is in-place and fast, but not stable.
- Object sorting supports stable ordering with comparators.
- '
Arrays.parallelSorthelps mainly for large workloads on multi-core systems.' - Comparator correctness and realistic benchmarking are critical for reliable sort decisions.
Related reading
- Sorting algorithm to implement highest total combinations
- Sorting algorithm to keep equal values separated
- sorting algorithm where pairwise-comparison can return more information than -1, 0, 1
- Sorting algorithms for data of known statistical distribution?
- Sorting an almost sorted array elements misplaced by no more than k
- Sorting an array in C?
- Sorting HashMap by values
- Spark from_avro() dataframe.show() errors java.lang.ArrayIndexOutOfBoundsException

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.