What is the space complexity of the python sort?

前端 未结 2 1671
深忆病人
深忆病人 2021-01-12 11:19

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

2条回答
  •  不思量自难忘°
    2021-01-12 11:50

    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)

提交回复
热议问题