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.
Answer from NPE on Stack OverflowProvided 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.
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.
sorting - What is the space complexity of the python sort? - Stack Overflow
What is the time complexity of the .sort() function in Python?
Sorting lists in python: sorted() vs sort()
What time complexity should one assume when using built in sort functions?
Videos
Space complexity is defined as how much additional space the algorithm needs in terms of the N elements. And even though according to the docs, the sort method sorts a list in place, it does use some additional space, as stated in the description of the implementation:
timsort can require a temp array containing as many as N//2 pointers, which means as many as 2*N extra bytes on 32-bit boxes. It can be expected to require a temp array this large when sorting random data; on data with significant structure, it may get away without using any extra heap memory.
Therefore the worst case space complexity is O(N) and best case O(1)
Python's built in sort method is a spin off of merge sort called Timsort, more information here - https://en.wikipedia.org/wiki/Timsort.
It's essentially no better or worse than merge sort, which means that its run time on average is O(n log n) and its space complexity is Ω(n)