9

According to Wikipedia:

"In Java, the Arrays.sort() methods use merge sort or a tuned quicksort depending on the datatypes and for implementation efficiency switch to insertion sort when fewer than seven array elements are being sorted"

But why? Both merge sort and quick sort are O(n log n).

3

5 Answers 5

10

Where the algorithms differ is their typical case behavior and this is where insertion sort is one of the worst. On the other hand, for very small collections (n ≈ n2) insertion sort's simplicity wins.

Java's algorithm selection rules prefer QuickSort first, and only fall back to something else due to specific restrictions. QuickSort, namely, is an unstable sort and thus is only acceptable for the sorting of primitives. For reference types, TimSort is used as of OpenJDK 7 (previously MergeSort).

Sign up to request clarification or add additional context in comments.

2 Comments

Additional note: Insertion sort wins on small collections because it has a lower constant factor, which big-O doesn't record. Note that, for example: n^2 < 10n for n < 10.
@Dukeling Yes, that's my point. Simplicity means low constant factor.
1

It's not that ad-hoc:

Arrays.java's sort method uses quicksort for arrays of primitives and merge sort for arrays of objects.

Why does Java's Arrays.sort method use two different sorting algorithms for different types?

Also, according to the docs:

For example, the algorithm used by sort(Object[]) does not have to be a mergesort, but it does have to be stable.

And another quote from the javadoc:

This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

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 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).

Comments

1

Quicksort and mergesort are both O(n log n) in average performance. In the worst case, quicksort is O(n^2).

2 Comments

Their average case is not O(n^2),, its O(nlogn).
Thanks for catching that. I was focused on the n^2 worst-case, and typed it accidentally for average.
0

They moved to tuned merge sort because found that statistically for real partially sorted data sets this method may be a little faster than quick sort.

3 Comments

Java never used MergeSort for primitives, and never used QuickSort for reference types.
Who is they? References, please.
That is for Collections.sort. Also, it said as fast as. I believe the main reason is still stability, though.
0

Well Java 7 uses Timsort - a hybrid of Merge and insertion. As a matter of fact it's widely used - android, Octave,python use it too. http://en.wikipedia.org/wiki/Timsort

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.