What sort does Java Collections.sort(nodes) use?

后端 未结 4 1245
耶瑟儿~
耶瑟儿~ 2020-12-01 10:11

I think it is MergeSort, which is O(n log n).

However, the following output disagrees:

-1,0000000099000391,0000000099000427
1,0000000099000427,000000         


        
4条回答
  •  醉话见心
    2020-12-01 10:24

    You sorted four nodes, so you didn't get merge sort; sort switched to insertion sort.

    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. (Wikipedia, emphasis added)

    Arrays.sort is used indirectly by the Collections classes.

    A recently accepted bug report indicates that the Sun implementation of Java will use Python's timsort in the future: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6804124

    (The timsort monograph, linked above, is well worth reading.)

提交回复
热议问题