简体   繁体   English

"Java stream().sorted 或 list.sort() 不会增加时间复杂度吗?"

[英]Does Java stream().sorted or list.sort() not increases time complexity?

While making some sortings, some people suggest using stream().sorted<\/code> or list.sort()<\/code> methods in Java to reduce time complexity.在进行一些排序时,有些人建议在 Java 中使用stream().sorted<\/code>或list.sort()<\/code>方法来降低时间复杂度。 However, I think these methods also use some sorting algorithms that has a similar time complexity.但是,我认为这些方法也使用了一些具有相似时间复杂度的排序算法。

List result = list.stream().sorted((o1, o2)->o1.getItem().getValue().
                                   compareTo(o2.getItem().getValue())).
                                   collect(Collectors.toList());

Sorting algorithms that use nested loops are usually O(n²)<\/code> complexity, due to every iteration of the inner loop occurring for every iteration of the outer loop.使用嵌套循环的排序算法通常是O(n²)<\/code>复杂度,因为内循环的每次迭代都发生在外循环的每次迭代中。 Some of these algorithms would be其中一些算法将是

  • Insertion Sort;插入排序;<\/li>
  • Bubble Sort;冒泡排序;<\/li>
  • etc;等等;<\/li><\/ul>

    However, other do not use nested loops as you mentioned, for example, the most used sorting algorithm for general use would be QuickSort, which stands as a Divide and Conquer Algorithm, it starts by selecting a pivot value and rearranging the array based on said pivot value, with all smaller values coming before it, and all larger values coming after it (Not necessarily ordered yet).但是,其他不使用您提到的嵌套循环,例如,最常用的一般排序算法是 QuickSort,它代表分治算法,它首先选择一个枢轴值并根据所述重新排列数组枢轴值,所有较小的值都在它之前,所有较大的值都在它之后(不一定排序)。

    QuickSort stands as O(nlog(n))<\/code> Average case complexity.快速排序为O(nlog(n))<\/code>平均案例复杂度。 Do bear in mind that some of the more advanced sorting algorithms also have a worst case scenario complexity of O(n^2)<\/code> .请记住,一些更高级的排序算法也具有O(n^2)<\/code>的最坏情况复杂度。

    In fact, some sorting implementations actually shuffle the array before sorting it just to avoid the worst case scenario.事实上,一些排序实现实际上在排序之前对数组进行了洗牌,只是为了避免最坏的情况。

    I leave here some information about Quicksort below.我在下面留下了一些关于 Quicksort 的信息。

    Quicksort explanation and how it works<\/a>快速排序说明及其工作原理<\/a>

    "

The very first impotent thing<\/strong> that you have to understand about any kind of algorithm is that just talking<\/strong> about algorithms will not significantly change your knowledge<\/strong> in this area.关于任何一种算法,你必须了解的第一件无能为力的事情<\/strong>是,仅仅谈论<\/strong>算法不会显着改变你在这方面的知识<\/strong>。

You have to study and practice them<\/code> , implementing algorithms step by step.您必须study and practice them<\/code> ,逐步实现算法。

If want to improve your understanding of sorting algorithms you need to start with the very basic like bubble sort<\/code><\/strong> and insertion sort<\/code><\/strong> (descriptions and pseudocode could've found in Wikipedia<\/code><\/strong> is quite enough to grasp the idea and start coding).如果想提高您对排序算法的理解,您需要从bubble sort<\/code><\/strong>和insertion sort<\/code><\/strong>等非常基本的开始( Wikipedia<\/code><\/strong>中的描述和伪代码足以掌握这个想法并开始编码)。 When you'll have a firm understanding of the basics that you ready to go with merge sort<\/code><\/strong> and quicksort<\/strong><\/a> .当您对merge sort<\/code><\/strong>和快速排序<\/strong><\/a>的基础知识有了深入的了解时。

Here is a small insight<\/strong><\/a> on what is happening inside the so-called dual-pivot quicksort<\/strong> provided by JDK.下面是关于 JDK 提供的所谓的双轴快速排序<\/strong>内部发生的事情的一个小见解<\/strong><\/a>。

In fact, it's a very advanced compound algorithm<\/code><\/strong> which apart from the conventional quicksort entails merge sort<\/code> , heap sort<\/code> , and parallel computing<\/code> in case of a massive data set.事实上,它是一种非常advanced compound algorithm<\/code><\/strong> ,除了传统的快速排序之外,还需要merge sort<\/code> 、 heap sort<\/code>和在海量数据集的情况下进行parallel computing<\/code> 。

Only when you know all proc and cons<\/code> of the essential algorithms (like why there are different portition schemes out there, why quicksort is preferred over merge sort meanwhile in the worse case time complexity of the merge sort seems better, etc) it make sense to speculate about the optimizations<\/code> that are going the dual-pivot quicksort<\/strong> .只有当您了解基本算法的所有优点proc and cons<\/code>时(例如为什么存在不同的分区方案,为什么快速排序优于合并排序,同时在更坏的情况下合并排序的时间复杂度似乎更好等)才有意义推测双轴快速排序<\/strong>的optimizations<\/code> 。

For now short answer with regards to the performance of the build-in quicksort:现在简短回答关于内置快速排序的性能:

guarantees n*log(n) on any data<\/strong>保证任何数据的 n*log(n)<\/strong>

<\/blockquote>

Now about Streams and sorting.<\/code><\/strong>

Sorting is always happening inside the array<\/code> in place.排序总是发生inside the array<\/code> 。 If the source of data is backed by the array an ArrayList<\/code> or CopyOnWriteArrayList<\/code> , then its underlying array will get sorted.如果数据源由ArrayList<\/code>或CopyOnWriteArrayList<\/code>数组支持,则其底层数组将被排序。

Otherwise, for instance, if you are dialing with a LinkedList<\/code> , a new array will be allocated in memory and all the data from the list will be copied to this array and then get sorted.否则,例如,如果您使用LinkedList<\/code>拨号,则会在内存中分配一个新数组,并且列表中的所有数据都将复制到该数组中,然后进行排序。

And when you are dialing with a stream<\/code> it'll not affect the source<\/code> of data.当您使用stream<\/code>拨号时,它not affect the source<\/code> 。 The whole data from the source requires to be loaded into memory to be sorted.来自源的全部数据需要加载到内存中进行排序。 And then you most likely will store it in a brand collection.然后您很可能会将其存储在品牌系列中。

Conclusion:<\/strong>结论:<\/strong>

The time complexity of these methods is nlog(n) .这些方法的时间复杂度为nlog(n) And it's the best time complexity for sorting an array.这是排序数组的最佳时间复杂度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM