Big-O complexity of java.util.stream.Stream.sorted()

前端 未结 2 1813
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 20:12

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

2条回答
  •  梦毁少年i
    2020-12-18 20:44

    As of JDK 8, the main sorting algorithm which is also used in standard stream API implementation for sequential sorting is TimSort. Its worst case is O(n log n), but it works incredibly fast (with O(n) and quite small constant) if data is presorted (in forward or reverse direction) or partially presorted (for example, if you concatenate two sorted lists and sort them again).

提交回复
热议问题