In collections class have a method sort() using for sort the collection elements, but I have one doubt, internally which sorting algorithm is used to sort the elements. I googled it but not got correct answer. help me please .
-
1The Open JDK source is available online. Try searching for "open jdk collections.sort". Following the source will redirect to List.sort which uses Arrays.sort - http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Arrays.java#Arrays.sort%28java.lang.Object%5B%5D%2Cjava.util.Comparator%29 with the "Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted.." as well as allow the 'specific' implementation to be explored in depth. – user2864740 Aug 27 '17 at 05:57
-
It's curious that 'mergeSort' will be "removed in a future release" and that [Timsort](https://en.wikipedia.org/wiki/Timsort) - "Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data." - is the going-forward impl. I'm not sure when this change occurred, but the source.. well, y'know :D – user2864740 Aug 27 '17 at 06:03
-
Interestingly, all the `Arrays.sort` methods that operate on arrays of primitive numeric types use a Dual-Pivot Quicksort, not Timsort. I'm curious about the difference--are sorts that involve only numbers that aren't part of a larger object more likely to involve a more random distribution and less likely to be partially sorted? – ajb Aug 27 '17 at 06:11
-
@AnilNivargi In Java 8u40 Timsort - I guess one could say "mergesortish"? - is the default, depending on the "java.util.Arrays.useLegacyMergeSort" option. The actual 'mergeSort' method is no longer called. I'm not entirely sure when this changed and am not particular motivated to look back in time. And, it's also JVM specific.. – user2864740 Aug 27 '17 at 06:14
-
thanks for clearing doubt @user2864740 – Anil Nivargi Aug 27 '17 at 06:20
-
why -2 for this question? – Anil Nivargi Mar 14 '18 at 07:25
2 Answers
The answer depends on the implementation of Java that you are talking about, and the type of data structure you are sorting.
For example, the javadoc for Collections (in Java 6) states:
Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)
Having said that, the javadocs for different versions say this about the Collections::sort
methods:
Java 6:
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
For sorting "native arrays" (ex: ints) it uses a "quicksort" that defaults to insertion sort for small chunks/array sizes.
Java 7:
Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.
The implementation takes equal advantage of ascending and descending order in its input array, and can take advantage of ascending and descending order in different parts of the same input array. It is well-suited to merging two or more sorted arrays: simply concatenate the arrays and sort the resulting array.
The implementation was adapted from Tim Peters's list sort for Python ( TimSort). It uses techiques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.
This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
For sorting "native arrays" (ex: ints) it uses a "dual pivot quicksort."
Java 8:
The Collections
javadoc defers to List::sort which says the same thing as the Java 7 text quoted above. Java 8's "dual pivot quicksort" had a few minor enhancements, apparently.
Java 9:
No change
Java 10:
More enhancement's have been proposed for dual pivot quicksort.
Also note that things may be different for other sort
APIs; e.g. those in the Arrays
class that are tailored for arrays of primitives. For example Java 9 / Arrays::sort(int[])
says:
Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
And the parallelSort
methods are different again.
The bottom line is that if you want to be sure which algorithm is used, look at the source code for the version of Java that you are using.
According to documentation (emphasis mine):
Implementation note: This implementation is a stable, adaptive, iterative mergesort ...

- 10,641
- 12
- 47
- 85