Why does java.util.Arrays.sort(Object[]) use 2 kinds of sorting algorithms?

后端 未结 4 1594
一生所求
一生所求 2020-12-23 20:42

I found that java.util.Arrays.sort(Object[]) use 2 kinds of sorting algorithms(in JDK 1.6).

pseudocode:

if(array.length<7)
   inserti         


        
4条回答
  •  执念已碎
    2020-12-23 21:31

    Quoted from: http://en.wikipedia.org/wiki/Insertion_sort

    Some divide-and-conquer algorithms such as quicksort and mergesort sort by 
    recursively dividing the list into smaller sublists which are then sorted. 
    A useful optimization in practice for these algorithms is to use insertion 
    sort for sorting small sublists, where insertion sort outperforms these more 
    complex algorithms. The size of list for which insertion sort has the advantage 
    varies by environment and implementation, but is typically between eight and 
    twenty elements.
    

提交回复
热议问题