python – What is the complexity of the sorted() function?

python – What is the complexity of the sorted() function?

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.

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.

python – What is the complexity of the sorted() function?

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 its said, no comparison sort can guarantee a time complexity smaller than lg(N!) ~ N log N.

Leave a Reply

Your email address will not be published. Required fields are marked *