What is the complexity of the sorted() function?

PythonAlgorithmSorting

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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.

Solution 4 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRead QView Question on Stackoverflow
Solution 1 - PythonNPEView Answer on Stackoverflow
Solution 2 - PythonblackmathView Answer on Stackoverflow
Solution 3 - PythonLerner ZhangView Answer on Stackoverflow
Solution 4 - PythonFahad NadeemView Answer on Stackoverflow