java.util.Collections.sort()方法的时间复杂度是多少?
作者:互联网
我写了以下课程:
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;
}
}
然后,在我的main()方法中,我创建了一个List,我添加了一些具有“X”和“angle”字段的对象.
然后我用:
Collections.sort(list, new SortingObjectsWithAngleField());
这种排序方法的复杂性是什么?
解决方法:
您可以阅读有关集合排序的文档,但这里适合您:
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.
你的比较器并没有改变这种复杂性,除非你对你的集合中的循环做任何事情,你不这样做.
标签:java,sorting,collections,time-complexity 来源: https://codeday.me/bug/20191001/1838640.html