Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complexity of Python Sort Method [duplicate]

If I have to sort some list, say a, using the sort method in Python such as below..

a=[3,7,1,0,2,8]
a.sort()
print a

What are the worst, average and best cases of such programs in case of sorting ? And what complexities would they have in each ? What sorting technique does python use in this ?

like image 357
h8pathak Avatar asked Sep 12 '14 17:09

h8pathak


1 Answers

Python uses Timsort, which was named after Tim Peters, the Python developer who invented it. The Wikipedia page has complexity information:

Worst case performance  O(nlogn)
Best case performance   O(n)
Average case performance    O(nlogn)
Worst case space complexity O(n)
like image 79
asmeurer Avatar answered Oct 09 '22 06:10

asmeurer