Collections.Sort 后续排序的性能?

发布于 2024-12-10 20:09:46 字数 315 浏览 1 评论 0原文

我将 Collections.sort 与自定义比较器类一起使用。我听说这有 O(N log N) 运行时复杂度。我很想知道当集合没有改变时后续排序会发生什么。

举例来说,假设我有一个 Egg 的 ArrayList,每个都有一个近似的 size 字段(我的比较器根据该字段进行排序)。如果我将十个鸡蛋插入数组列表中并对其进行排序,则预计需要 O(N log N) 时间。

如果我再次排序,而不添加、删除或更改任何元素,是否仍需要 N log N 时间?

I'm using Collections.sort with a custom comparator class. I've heard that this has O(N log N) runtime complexity. I'm curious to know what happens on subsequent sorts when the collection hasn't changed.

By example, lets say I have an ArrayList of Eggs, each which has an approximate size field (which my comparator sorts by). If I insert ten eggs into the array list, and sort it, I can expect it to take O(N log N) time.

If I sort it again, without adding, removing, or changing any elements, will it still take N log N time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海螺姑娘 2024-12-17 20:09:46

The Javadoc says 'the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist'. That appears to mean nothing happens so it should be quicker.

You could always test it.

稀香 2024-12-17 20:09:46

我还没有分析过当前sun java库中的代码。然而,javadoc 声明使用了合并排序。大多数合并排序在已排序的集合上产生 O(n) 性能。尽管文档中没有说明这一点。我的个人经验表明我在排序或接近排序的列表上表现非常好。

I have not analysed the code in the current sun java library. However, the javadoc states that a merge sort is used. Most merge sorts yield a O(n) performance on already sorted collection. Although this is not stated in the documentation. My personal experience has shown me really good performance on sorted or nearly sorted lists.

在你怀里撒娇 2024-12-17 20:09:46

Per JavaDoc Collections.sort 使用合并排序算法。

您可以在这里亲自查看它的效果 -> http://www.sorting-algorithms.com/

Per JavaDoc Collections.sort uses merge sort algorithm.

You can see how it does, for yourself, here -> http://www.sorting-algorithms.com/

深空失忆 2024-12-17 20:09:46

为了扩展 EJP 的答案,如果文档表明合并过程是跳过的步骤,那么在这种最佳情况下,运行时将为 LG N,因为它仍然会将列表分解为 LG N 子问题。相对于线性扫描的倍增就是效率的提升。

To expand on EJP's answer, if the documentation indicates that the merge pass is the step skipped, then in this best case runtime would be LG N because it will still break the list into LG N subproblems. The multiplication against the linear scan is the improvement in efficiency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文