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.
What time complexity should one assume when using built in sort functions?
What is the time complexity of Python sorted()? - TestMu AI Community
python - What is the time complexity of the following function, that uses buit-in sorted function? - Stack Overflow
Complexity of Python Sort Method - Stack Overflow
Videos
Let's say I want to find a number in an unordered array in O(log n) time. Implementing binary search is already O(log n) time but because I have to sort the array first do we take that into account in the time complexity calculations?
The problem gets worse if you're using a built in sort that you don't know the time complexity, for example, if you were doing this in JavaScript [5,4,12,3,56,1, ...].sort() what time complexity would you attribute to it?
Python built-in sorting function is a custom sorting function called Timsort with a time complexity of O(n log n).
More on that in Wikipedia here
Sort algorithms are usually between O(n log(n)) up to O(nยฒ). Built-in sort functions are usually O(n log(n)). Your for loop is also O(n) and as a result the time complexity of your algorithm is O(n log(n)) + n) which is O(n log(n)).
And because you're creating a new array in the first line, the memory complexity is O(n)
In addition you can use a hash table to improve both time and memory complexity.