爪哇 - Collections.sort()的性能 [英] Java - Collections.sort() performance

查看:224
本文介绍了爪哇 - Collections.sort()的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用<一个href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort%28java.util.List%29">Collections.sort()排序在LinkedList,它的元素实现Comparable接口,因此它们按自然顺序。在Javadoc文档它说,这种方法使用的归并的算法至极具有的n * log(n)性能。

Im 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 said this method uses mergesort algorithm wich has n*log(n) performance.

我的问题是,如果有一个更有效的算法进行排序我的LinkedList?

My question is if there is a more efficient algorithm to sort my LinkedList?

这是列表的规模可能会非常高,排序也将是非常频繁的。

The size of that list could be very high and sort will be also very frequent.

谢谢!

推荐答案

O(N日志N)是非常好的渐近。这就是说,有线性时间 O(N)非基于比较的排序,如计数排序和桶排序。这是非常有用的时候,例如你排序整数数以百万计,但他们1..10之间。

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.

  • Wikipedia/Counting sort
  • Wikipedia/Bucket sort

这篇关于爪哇 - Collections.sort()的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆