Why does Java 6 Arrays#sort(Object[]) change from mergesort to insertionsort for small arrays?

限于喜欢 提交于 2019-11-28 23:04:39

Yes this is intentional. While the Big-O of mergesort is less than that of quadratic sorts such as insertion sort, the operations it does are more complex and thus slower.

Consider sorting an array of length 8. Merge sort makes ~14 recursive calls to itself in addition to 7 merge operations. Each recursive call contributes some non-trivial overhead to the run-time. Each merge operation involves a loop where index variables must be initialized, incremented, and compared, temporary arrays must be copied, etc. All in all, you can expect well over 300 "simple" operations.

On the other hand, insertion sort is inherently simple and uses about 8^2=64 operations which is much faster.

Think about it this way. When you sort a list of 10 numbers by hand, do you use merge sort? No, because your brain is much better at doing simple things like like insertion sort. However if I gave you a year to sort a list of 100,000 numbers, you might be more inclined to merge sort it.

As for the magic number 7, it is empirically derived to be optimal.

EDIT: In a standard insertion sort of 8 elements, the worst case scenario leads to ~36 comparisons. In a canonical merge sort, you have ~24 comparisons. Adding in the overhead from the method calls and complexity of operations, insertion sort should be faster. Additionally if you look at the average case, insertion sort would make far fewer comparisons than 36.

Insertion sort is n(n-1)/2 and merge sort is n*(log n with base 2 ).

Considering this -

  1. For Array of Length 5 => Insetion sort = 10 and merge sort is 11.609
  2. For Array of Length 6 => Insetion sort = 15 and merge sort is 15.509
  3. For Array of Length 7 => Insetion sort = 21 and merge sort is 19.651
  4. For Array of Length 8 => Insetion sort = 28 and merge sort is 24

From above data it is clear, till length 6, insetion sort is faster and after 7, merge sort is efficient.

That explains why 7 is used.

My understanding is that this is an empirically derived value, where the time required for an insertion sort is actually lower, despite a (possible) higher number of comparisons required. This is so because near the end of a mergesort, the data is likely to be almost sorted, which makes insertion sort perform well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!