java中集合#排序方法的时间复杂度是多少?[英] What is the time complexity of Collections#sort method in Java?

本文是小编为大家收集整理的关于java中集合#排序方法的时间复杂度是多少?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

Java中Collections#sort方法的时间复杂性是什么?使用了哪种算法?

是Collection#sort对10^6的ArrayList排序的好方法?

推荐答案

这取决于您使用的Java版本.,但最终,Big-O时间复杂性仍然是O(n*log(n)).

对于Java 6,它是Mergesort的修改版本.在此处查看描述: Collections#sort Java 6

排序算法是一种修改的顺序(如果低符号中的最高元素小于高sublist中的最低元素,则省略合并).该算法提供了保证的n log(n)性能.指定的列表必须可修改,但不需要解析.该实现将指定的列表转载到数组中,对数组进行分组,并在列表上迭代从数组中的相应位置重置每个元素.这避免了试图对链接列表进行分类的N2 log(n)性能.

对于Java 7,它得到了改进: Collections#sort java 7 由于增强.请注意, timsort 具有O(n)的最佳案例,并且证明比以前的实现更快.

实施注意:此实现是一种稳定,自适应,迭代的合并器,当输入数组被部分排序时,需要远远超过n lg(n)比较,而当输入阵列随机排序时提供传统合并的性能.如果几乎将输入阵列分类,则实现大约需要n个比较.临时存储要求从几乎排序的输入阵列的小常数到随机排序输入阵列的N/2对象引用.

实现在其输入数组中具有同等的优势,可以在同一输入数组的不同部分中利用上升和下降顺序.它非常适合合并两个或多个排序的数组:只需将阵列连接并排序所得的数组.

该实施是根据python的蒂姆·彼得斯(Tim Peters)的列表进行了改编的(.

.

此实现将指定的列表转载到数组中,对数组进行分类,然后在列表上迭代从数组中的相应位置重置每个元素.这避免了试图对链接列表进行分类的N2 log(n)性能.


这是对10^6的ArrayList排序的好方法?

从理论上讲,它足以使用.但这让我想知道为什么您必须在内存中对数据进行分类.如果数据来自数据库,请使用索引列/字段在此处对其进行排序,否则请检查您是否知道将用于排序的字段的某些特征,并且是否可以使用O(n)时间复杂性算法,例如 bucket stort radix排序.如果没有其他方法,请使用Collections#sort.

其他推荐答案

collections.sort()的时间复杂性为o(n*log(n)),并且仅在调用sort()的列表(). >

收集文档中存在的信息 -

排序算法是一种修改的顺序(如果最高元素,则省略合并 在低sublist中,小于高sublist中的最低元素). 该算法提供保证的n log(n)性能.

本文地址:https://www.itbaoku.cn/post/978641.html

问题描述

What is the time complexity of Collections#sort method in Java? Which algorithm is used?

Is Collection#sort a good method for sorting an ArrayList of 10^6?

推荐答案

This depends on the version of Java you use. But in the end, the Big-O time complexity is still O(N*log(N)).

For Java 6, it's a modified version of mergesort. Check the description here: Collections#sort for Java 6

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.

For Java 7, it was improved: Collections#sort for Java 7 due to enhancement. Note that TimSort has a best case of O(N) and proves to be faster than the previous implementation.

Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

The implementation takes equal advantage of ascending and descending order in its input array, and can take advantage of ascending and descending order in different parts of the same input array. It is well-suited to merging two or more sorted arrays: simply concatenate the arrays and sort the resulting array.

The implementation was adapted from Tim Peters's list sort for Python ( TimSort). It uses techiques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.

This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.


Is this a good method for sorting an ArrayList of 10^6?

In theory, it is enough to use. But this makes me wonder why would you have to sort the data in memory. If the data comes from a database, then sort it there using an indexed column/field, otherwise check if you know some characteristics of the field you will use for sorting and if you may use a O(N) time complexity algorithm like Bucket Sort or Radix Sort. When there's no other way, use Collections#sort.

其他推荐答案

The time complexity of Collections.sort() is O(n*log(n)) and a list sorted with Collections.sort() will only be sorted after the call to sort().

Information present in collections documentation -

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance.