Java中的Collections#sort方法的时间复杂度是多少_java collections.sort 耗时-程序员宅基地

技术标签: 算法  Java基础  

参考:https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29
https://stackoverflow.com/questions/4254122/what-is-the-time-complexity-of-java-util-collections-sort-method

这取决于您使用的Java版本.但是最后,Big-O时间复杂度仍然是O(N * log(N)).
对于Java 6,它是mergesort的修改版本.在这里查看说明: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.

对于Java 7,已进行了改进:由于enhancement而导致的改进为:Collections#sort for Java 7.请注意,TimSort具有O(N)的最佳情况,并且被证明比以前的实现更快.

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 ( 07004). 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?

从理论上讲,使用就足够了.但这使我想知道为什么您必须对内存中的数据进行排序.如果数据来自数据库,则使用索引的列/字段在那里进行排序,否则检查是否知道要用于排序的字段的某些特征,以及是否可以使用O(N)时间复杂度算法,例如Bucket Sort或Radix Sort.如果没有其他方法,请使用Collections#sort.

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_32907195/article/details/108576224

智能推荐

宽字符与窄字符-程序员宅基地

文章浏览阅读194次。<一>什么是宽字符与窄字符(1) 一个ANSI字符占一个字节共8位,一个UNICODE字符占两个字节共16位;ANSI字符串以’\0’结束,0x00。#Q:UNICODE字符串以什么结束??#A:UNICODE字符串以L”\0”结束,0x0000。(2)UNICODE和ANSI字符的相关定义及应用在各种运行库中的体现如下:1) 在C标准库中i. UNICODE在C标准库..._c语言中宽字符串和窄字符串

人工智能专题:中国人工智能艺术教育白皮书最新-程序员宅基地

文章浏览阅读898次,点赞15次,收藏23次。今天分享的是深度研究报告:《页。_中国人工智能艺术教育白皮书

IDEA设置Maven下载source、document_maven download source-程序员宅基地

文章浏览阅读6.4w次,点赞19次,收藏37次。1、打开Maven设置2、在import设置中勾选source和document3、对原项目进行重新下载,打开右侧Maven projects,选中所有项目模块之后,点击download sources and/or documentation,即可重新下载依赖jar的源码及文档4、下载之后结果如下..._maven download source

Java基础知识篇(第一篇)_java第一篇基础知识-程序员宅基地

文章浏览阅读169次,点赞2次,收藏2次。public class Class_1 { public static void main(String[]args) { //主函数 char one='一';//定义一个变量为char型并赋值为:一 char(字符型用于存储单个字符,如'男','女'等单个字符) String two="两本";//定义一个变量为String型并赋值为:两本 String(字符串型用于存储一串字符,如"今天天气不错呢!"等) double f..._java第一篇基础知识

小说更新太慢怎么办_5本更新慢如龟速的网络小说,书虫追更很煎熬,却依旧不离不弃...-程序员宅基地

文章浏览阅读2.2k次。对于爱看小说的老书虫而言,找个好小说看真的太难了。所以老书虫们往往认准一些实力派作者,只有某些作者的书才能够完整看下去。但让老书虫郁闷的是,一些实力派作者偏偏断更、拖更、龟速更新,让人郁闷无比。郁闷归郁闷,追更还是不能停下来。我是真游泳的猫,一个看书16年的老书虫。关注我,今天我和大家聊聊5本更新慢如龟速的网络小说,书虫追更很煎熬,却依旧不离不弃。第5名,发飙的蜗牛《妖神记》。据说当年蜗牛是网文游..._仙草小说更新慢

2021-06-25(138. 复制带随机指针的链表)-程序员宅基地

文章浏览阅读52次。/*// Definition for a Node.class Node { int val; Node next; Node random; public Node(int val) { this.val = val; this.next = null; this.random = null; }}*/class Solution { public Node copyRandomList(Nod

随便推点

如何在myeclipse中创建tld文件-程序员宅基地

文章浏览阅读377次。如何在myeclipse中创建tld文件new->file->xxx.tld这个我会我想要是直接创建tld文件 然后会自己产生代码 :( _myeclipse tld在哪

c# 用Dictionary实现日志数据批量插入_c# dictionary 批量-程序员宅基地

文章浏览阅读316次。背景最近再做一个需求,就是对站点的一些事件进行埋点,说白了就是记录用户的访问行为。那么这些数据怎么保存呢,人家点一下保存一下?显然不合适,肯定是需要批量保存,提高效率。问题窥探首先,我想到的是Dictionary,对于C#中的Dictionary类相信大家都不陌生,这是一个Collection(集合)类型,可以通过Key/Value(键值对的形式来存放数据;该类最大的优点就是它查找元素的时间复杂度接近O(1),实际项目中常被用来做一些数据的本地缓存,提升整体效率。Dictionary是非线程安全的类型_c# dictionary 批量

【C语言】指针进阶知识终章_((int)(main & sub)) != 0-程序员宅基地

文章浏览阅读1.1k次,点赞64次,收藏45次。本篇博客涉及内容相对来说较多:刚开始:有趣的2个代码不同方法模拟实现简单计算器函数指针数组指向函数指针数组的指针回调函数冒泡排序较通用版qsort函数_((int)(main & sub)) != 0

微生物组统计和可视化——phyloseq入门-程序员宅基地

文章浏览阅读2.2w次,点赞22次,收藏90次。翻译:文涛写在前面: 最近一段时间面临着各种各样的问题和挑战,总在寻求一种可以权衡,理解的解释的解决之道。phyloseq:使用R语言分析微生物群落(microbiome census ..._phyloseq

非对称加密算法_利用非对称的密码方法解决了交易数据传输过程中的保密问题-程序员宅基地

文章浏览阅读1.6k次。 非对称加密算法是一种密钥的保密方法。 非对称加密算法需要两个密钥:公开密钥和私有密钥。公开密钥与私有密钥是一对,若果用公开密钥对数据进行加密,只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫做非对称密钥算法。 非对称加密算法实现机密信息交换的基本过程是:甲方生成一对密钥并将其中的一把作为公用密钥向其他用户公开;得到该公用密钥的乙方使用该密钥对机..._利用非对称的密码方法解决了交易数据传输过程中的保密问题

前端工具推荐 PxCook-程序员宅基地

文章浏览阅读1w次,点赞29次,收藏41次。前端页面设计的工具推荐---PxCook_pxcook

推荐文章

热门文章

相关标签