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

后端 未结 3 1615
慢半拍i
慢半拍i 2020-12-15 04:42

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 th

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 05:32

    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.

提交回复
热议问题