Java 中 Arrays.Sort 方法的运行时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21219777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:25:15  来源:igfitidea点击:

The Running Time For Arrays.Sort Method in Java

javaperformance

提问by user3212622

Does anyone know the running time in big O notation for the arrays.sort java method? I need this for my science fair project.

有谁知道 array.sort java 方法的大 O 表示法的运行时间?我的科学博览会项目需要这个。

采纳答案by pinkpanther

From official docs

来自官方文档

I've observed that there are primarily two approaches. So, it depends on what you are sorting and what overloaded method from sortfamily of methods you are calling.

我观察到主要有两种方法。因此,这取决于您要排序的内容以及sort您正在调用的方法系列中的重载方法。

Docs mention that for primitive typessuch as long, byte(Ex: static void sort(long[])):

文档提到对于诸如long, byte(Ex :) 之类的原始类型static void sort(long[])

The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.

排序算法是一种经过调整的快速排序,改编自 Jon L. Bentley 和 M. Douglas McIlroy 的“设计排序功能”,软件实践和经验,卷。23(11) P. 1249-1265(1993 年 11 月)。该算法在许多数据集上提供 n*log(n) 性能,导致其他快速排序降级为二次性能。

For Object types:(Ex: void sort(Object list[]))

对象类型:(例如:void sort(Object list[])

Guaranteed O(nlogn) performance

保证 O(nlogn) 性能

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance.

排序算法是一种改进的归并排序(如果低子列表中的最高元素小于高子列表中的最低元素,则省略合并)。该算法提供有保证的 n*log(n) 性能。

Hope that helps!

希望有帮助!

回答by Svetlin Zarev

Arrays.sort()uses Tim sort- O(N log N) for array of objects and QuickSortfor arrays of primitives - again O(N log N).

Arrays.sort()使用Tim sort- O(N log N) 用于对象QuickSort数组和基元数组 - 同样是 O(N log N)。

Here is an awesome comparison of sorting algorithms: http://www.sorting-algorithms.com/

这是排序算法的一个很棒的比较:http: //www.sorting-algorithms.com/