[英]What is the time complexity of java.util.Collections.sort() method?
I have written the following class:我写了以下课程:
public class SortingObjectsWithAngleField implements Comparator<Point> {
public int compare(Point p1, Point p2) {
double delta = p1.getAngle() - p2.getAngle();
if(delta == 0.00001)
return 0;
return (delta > 0.00001) ? 1 : -1;
}
}
Then, in my main()
method, I have created a List
to which I add some objects which has "X" and "angle" field.然后,在我的
main()
方法中,我创建了一个List
,我向其中添加了一些具有“X”和“角度”字段的对象。
I then use:然后我使用:
Collections.sort(list, new SortingObjectsWithAngleField());
What is the complexity of this sort method?这种排序方法的复杂性是多少?
You could have read up the docs on Collections sort, but here it is for you:您本可以阅读有关 Collections sort 的文档,但这里是为您准备的:
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) 性能。
<\/blockquote>
Your Comparator doesn't change this complexity, unless you do anything with loops over your collection in it, which you don't.您的 Comparator 不会改变这种复杂性,除非您对其中的集合执行任何循环操作,而您没有这样做。
"
您应该在 API 中找到它: n log(n)<\/a> 。
"Taken from Collections.sort -取自 Collections.sort -
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) 性能
Everyone has stated the API doc<\/a> , adding some more relevant information which I found.每个人都说明了
API 文档<\/a>,并添加了一些我发现的更多相关信息。
If you provide custom comparator, then a modified version of mergesort (also known as
timsort<\/code> ) is used.
如果您提供自定义比较器,则使用修改版本的合并排序(也称为
timsort<\/code> )。
The implementation has been borrowed from
list sort for python<\/a> .该实现已从
python 的列表排序中<\/a>借用。
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.