Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the space complexity of the python sort?

What space complexity does the python sort take? I can't find any definitive documentation on this anywhere

like image 505
Kamal Avatar asked Feb 13 '18 03:02

Kamal


People also ask

What is space complexity of sorting algorithms?

What Is Space Complexity? The space complexity of an algorithm describes the amount of memory an algorithm takes to run in terms of the characteristics of the input. In other words, we can say space complexity is the approximate total extra space required by the program to run.

Is Python sort constant space?

There is no built-in constant-space sort. can you describe more about your constraints? What is the nature / size of your data? Nobody is stopping you from writing your own sorting function.

Why is space complexity of selection sort O 1?

The space complexity of Selection Sort is O(1). This is because we use only constant extra space such as: 2 variables to enable swapping of elements. One variable to keep track of smallest element in unsorted array.


1 Answers

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)

like image 76
damores Avatar answered Oct 09 '22 14:10

damores