其他分享
首页 > 其他分享> > 排序 – Quicksort或O(N.logN)在jdk中排序

排序 – Quicksort或O(N.logN)在jdk中排序

作者:互联网

参见英文答案 > What sort does Java Collections.sort(nodes) use?                                    4个
在jdk标准库中是否有快速排序或其他O(N.logN)排序?

收藏课没有带来希望:

Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)

和Collections.sort()没有任何线索:

sort(List<T> list)
Sorts the specified list into ascending order,according to the natural ordering of its elements.

解决方法:

排序依赖于Arrays.sort.由于sort方法取决于类型,因此读取JavaDoc并不是那么明显.
根据阈值修改的合并排序或调整的快速排序:

 /**
 * Tuning parameter: list size at or below which insertion sort will be
 * used in preference to mergesort or quicksort.
 */
private static final int INSERTIONSORT_THRESHOLD = 7;

这是一篇比较quicksort和mergesort的文章:Quick Sort Vs Merge Sort

结论:除非您真正知道自己在做什么,否则请相信Java实现.

标签:java,sorting,standard-library
来源: https://codeday.me/bug/20190629/1328464.html