What is a sorting algorithm for Java

How java internally sorts data types and why? It would be great if specific algorithms were mentioned.

+5
source share
2 answers

Starting with version 7, the Oracle Java implementation uses Timsort for arrays of objects larger than 10 elements and Insert sort for arrays with fewer elements. The same considerations apply to both Arrays.sort()and to Collections.sort(). Older versions of Java used Merge sort instead of Timsort .

Other implementations of the language (except Oracle) may use a different sorting algorithm, since this is not provided for by the specification. Quoting the Collections' documentation :

The documentation for the polymorphic algorithms contained in this class usually includes a brief description of the implementation. Such descriptions should be considered as implementation notes, not part of the specification. Developers should boldly replace other algorithms, subject to the specification itself. (For example, the algorithm used by sorting should not be merging, but it should be stable.)

+14
source

Collections.sort()uses modified merge. Arrays.sort()uses quicksort variation for primitives and mergesort for sorting Object.

Java 7 @SebastianPaaskeTørholm

+5

All Articles