Java 6's mergesort implementation in Arrays.java
uses an insertion-sort if the array length is less than some threshold. This value is hard-coded to 7. As the algorithm is recursive, this eventually happens many times for a large array. The canonical merge-sort algorithm does not do this, just using merge-sort all the way down until there is only 1 element in the list.
Is this an optimisation? If so, how is it supposed to help? And why 7
? The insertion sort (even of <=7
things) increases the number of comparisons required to sort a large array dramatically - so will add cost to a sort where compareTo()
calls are slow.
(x-axis is size of array
, y-axis is # of comparisons
, for different values of INSERTIONSORT_THRESHOLD
)