java.util.stream.Stream的Big-O复杂性< T> .sorted() [英] Big-O complexity of java.util.stream.Stream<T>.sorted()

查看:196
本文介绍了java.util.stream.Stream的Big-O复杂性< T> .sorted()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道 java.util.stream.Stream< T> .sorted()的时间复杂度是什么?

Does anyone know what the time complexity of java.util.stream.Stream<T>.sorted() is?

推荐答案

那么, sorted()本身就是O(1),因为它是一个没有的中间操作使用流,但只是向管道添加一个操作。

Well, sorted() in itself is O(1), since it's an intermediate operation that doesn't consume the stream, but simply adds an operation to the pipeline.

一旦终端操作消耗了流,就会发生排序并且

Once the stream is consumed by a terminal operation, the sort happens and either


  • 它没有做任何事情(O(1)),因为流知道元素已经排序(例如,因为它们来自SortedSet)

  • 或者流不是并行的,它委托给 Arrays.sort()(O(n log n))

  • 或流是并行的,它委托给 Arrays.parallelSort()(O(n log n))

  • it doesn't do anything (O(1)) because the stream knows that the elements are already sorted (because they come from a SortedSet, for example)
  • or the stream is not parallel, and it delegates to Arrays.sort() (O(n log n))
  • or the stream is parallel, and it delegates to Arrays.parallelSort() (O(n log n))

这篇关于java.util.stream.Stream的Big-O复杂性&lt; T&gt; .sorted()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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