[ACCEPTED]-Python sorting complexity on sorted list-sorting

Accepted answer
Score: 16

This is entirely implementation dependent. All 12 that python guarantees is that the builtin 11 sorting algorithm is stable (elements which compare 10 equal retain their relative ordering). An 9 implementation could even use a stable bubble 8 sort if it wanted to ...

Cpython uses TimSort (a 7 hybrid of insertion sort an mergesort) which 6 I believe has O(N) complexity if the input 5 is already sorted -- It picks up the best 4 case performance of insertion sort and the 3 worst case performance of mergesort (O(NlogN)).

And 2 if you're curious about the implementation, the 1 source code has a very nice description.

Score: 1

Python's sort is called timsort. Its average case 4 performance is O(nlog(n)). It performs really 3 well with pre-sorted lists because it's 2 implementation is designed to seek out runs 1 of sorted elements in the list.

More Related questions