[英]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>。
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>
Conclusion:<\/strong>结论:<\/strong>
if you need to change a source of data - use the
sort()<\/code> method with the list directly;
如果您需要更改数据源 - 直接对列表使用
sort()<\/code>方法;
<\/li>
if your task doesn't require sorting all the initial data (or you simply want to preserve the initial state of the source) - then
Stream API<\/code> will give a hand with filtering and sorting.
如果您的任务不需要对所有初始数据进行排序(或者您只是想保留源的初始状态) - 那么
Stream API<\/code>将帮助您进行过滤和排序。
<\/li><\/ul>"
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.