Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is merge sort used by java to sort an array greater than elements 7

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

like image 426
joker Avatar asked May 16 '13 12:05

joker


People also ask

Why is merge sort better for large arrays?

Merge sort requires more space as it creates an extra array for storing, and no matter what it will compare every item. Quick sort on the other hand does not require extra space, and doesn't swap or compare more than necessary.

Why is merge sort better than selection sort?

Merge Sort is considered to be one of the fastest sorting algorithms, it is a bit more complex than Selection and Bubble Sort but its more efficient. The idea of Merge Sort is to divide the data-set into smaller data-sets, sort those smaller data-sets and then join them (merge them) together.

Why is merge sort faster?

Indeed, it is because merge sort is implemented recursively that makes it faster than the other algorithms we've looked at thus far.


3 Answers

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

like image 178
Marko Topolnik Avatar answered Nov 15 '22 15:11

Marko Topolnik


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

like image 20
zw324 Avatar answered Nov 15 '22 15:11

zw324


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

like image 34
Andy Thomas Avatar answered Nov 15 '22 15:11

Andy Thomas