What is the complexity of the sorted() function?

Question:

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 function?

Asked By: Read Q

||

Answers:

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.

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

Answered By: NPE

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.

Answered By: blackmath

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.

Answered By: Lerner Zhang

Just like in every other programming language, the sorted() has O(NlogN) Time Complexity.

Answered By: Fahad Nadeem

The time complexity in the average case would be O(nlog(n))

Answered By: yamero
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.