Java - Collections.sort() performance

后端 未结 5 1386
孤街浪徒
孤街浪徒 2020-12-30 02:59

I\'m using Collections.sort() to sort a LinkedList whose elements implements Comparable interface, so they are sorted in a natural order. In the javadoc documentation its sa

5条回答
  •  春和景丽
    2020-12-30 03:37

    O(N log N) is very good asymptotically. That said, there are linear time O(N) non-comparison based sort, e.g. counting sort and bucket sort. This is useful when, e.g. you're sorting millions and millions of integers, but they're between 1..10.

    Also, if the list is "almost sorted", the otherwise quadratic insertion sort is reported to actually be better under some scenarios.

    Whether or not this is applicable, or even worth to implement, depends on your profiling results. I'd say that unless it shows the sort to be a bottleneck, don't worry about it.

    See also

    • Wikipedia/Counting sort
    • Wikipedia/Bucket sort

    Related questions

    • Is there an O(n) integer sorting algorithm?

提交回复
热议问题