简体   繁体   中英

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.

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:

This algorithm offers guaranteed n log(n) performance.

您应该在 API 中找到它: n log(n)<\/a> 。

"

Taken from 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

Everyone has stated the API doc<\/a> , adding some more relevant information which I found.

The implementation has been borrowed from

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM