这个python排序方法的复杂度是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14434490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:29:41  来源:igfitidea点击:

What is the complexity of this python sort method?

pythonalgorithmsorting

提问by Read Q

I have a list of lists and I am sorting them using the following

我有一个列表列表,我正在使用以下方法对它们进行排序

data=sorted(data, key=itemgetter(0))

Was wondering what is the runtime complexity of this python method?

想知道这个 python 方法的运行时复杂度是多少?

采纳答案by NPE

Provided itemgetter(0)is O(1)when used with data, the sort is O(n log n)both on average and in the worst case.

设置itemgetter(0)O(1)当用于data,排序是O(n log n)两者平均,并在最坏的情况下。

For more information on the sorting method used in Python, see Wikipedia.

有关 Python 中使用的排序方法的更多信息,请参阅Wikipedia

回答by blackmath

sorted is like sort except that the first builds a new sorted list from an iterable while sort do sort in place. The main difference will be space complexity.

sorted 就像 sort ,除了第一个从一个可迭代对象构建一个新的排序列表,而 sort 进行就地排序。主要区别在于空间复杂度。

回答by Lerner Zhang

It is the Timsort, and Timsort is a kind of adaptive sorting algorithm based on merge sort and insertion sort, then I thought it belongs to the comparison sort, and it's said, no comparison sort can guarantee a time complexity smaller than lg(N!) ~ N log N.

就是Timsort,Timsort是一种基于归并排序和插入排序的自适应排序算法,后来我认为它属于比较排序,据说,没有比较排序可以保证时间复杂度小于lg(N! ) ~ N log N。