Java Arrays.Sort()
Ref: StackOverflow
As for Arrays.sort
, Oracle implementation works as follows:
- For
int
,long
,float
anddouble
there are essentially three algorithms: Insertion Sort, Dual-Pivot Stable Quicksort and Merge Sort.
1.1. If the array is large enough (currently larger than 286 elements) and it's not “almost sorted” (determined dynamically), then Merge Sort is used.
1.2. Otherwise, if the array is very small (currently less than 47 elements), then Insertion Sort is used.
1.3. Otherwise (if the array is not that small, but either smaller than 286 elements or mostly sorted), Dual-Pivot Quicksort is used.
- For
short
andchar
, there are Insertion Sort, Dual-Pivot Stable Quicksort and Counting Sort.
2.1. If the array is larger than a certain threshold (currently 3200 elements), then Counting Sort is used.
2.2. Otherwise, similarly to larger types, Insertion Sort or Dual-Pivot Stable Quicksort is used (using the threshold of 47 elements).
For bytes, it's like for shorts, but Counting Sort is used when there are more than 29 elements (since it doesn't require that much memory).
For reference types, things are complicated.
4.1. If the java.util.Arrays.useLegacyMergeSort
property is set, then some sort of legacy Merge Sort is used (surprisingly), which falls back to Insertion Sort on really small arrays (less than 7 elements).
4.2. Otherwise, TimSort is used (Tim Pieter's list sort for Python), which is something similar to Merge Sort, but for arrays of less than 32 objects no merging is performed.
The point of the above is that Java people really did their research instead of blindly implementing a random sorting algorithm, hoping that everyone will be happy with it.
In my experience, I find Arrays.sort
to be extremely efficient. I can think of two reasons to implement a custom algorithm: the aforementioned case using certain data properties, and sorting data that comes in different “parallel” arrays that we can't merge into one array of composite objects for whatever reason (performance, or perhaps we just don't have control over the code that produced those arrays).
Comments
Post a Comment