Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java 7 using Tim Sort for the Method Arrays.Sort?

I can't find the documentation of Java 7, I can only find about the Java 6, which is still quick or merge. Does anyone know how to find the documentation of the method Arrays.sort in Java 7?

like image 413
Osvaldo Avatar asked Oct 25 '10 19:10

Osvaldo


People also ask

Does Java use Tim sort?

Go uses insertion sort and stable minimum storage merging for stable sorts. Java uses dual-pivot sort for primitives, which is an unstable sort. Java uses timsort, a stable sorting algorithm for objects.

Which sorting is used in Arrays sort in Java?

As mentioned in the official JavaDoc, Arrays. sort uses dual-pivot Quicksort on primitives. It offers O(n log(n)) performance and is typically faster than traditional (one-pivot) Quicksort implementations.

Is Tim sort better than merge sort?

TimSort is a highly optimized mergesort, it is stable and faster than old mergesort. when comparing with quicksort, it has two advantages: It is unbelievably fast for nearly sorted data sequence (including reverse sorted data); The worst case is still O(N*LOG(N)).

Which sorting technique is best for array?

When the array is almost sorted, insertion sort can be preferred. When order of input is not known, merge sort is preferred as it has worst case time complexity of nlogn and it is stable as well. When the array is sorted, insertion and bubble sort gives complexity of n but quick sort gives complexity of n^2.


1 Answers

Java 7 uses Dual-Pivot Quicksort for primitives and TimSort for objects.

According to the Java 7 API doc for primitives:

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.

According to the Java 7 API doc for objects:

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.

Timsort is a hybrid "merge sort and insertion sort."

Not sure if this is much different from what it was in Java 6, for Arrays.sort JDK6:

a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993)

For Object[] or collections (Collections.sort()) merge sort is used.

like image 138
Michael Borgwardt Avatar answered Sep 24 '22 02:09

Michael Borgwardt