Why is mergesort space complexity O(log(n)) with linked lists?

前端 未结 2 1476
傲寒
傲寒 2021-02-01 05:28

Mergesort on an array has space complexity of O(n), while mergesort on a linked list has space complexity of O(log(n)), documented here

I believe that I understand the

相关标签:
2条回答
  • 2021-02-01 05:36

    The mergesort algorithm is recursive, so it requires O(log n) stack space, for both the array and linked list cases. But the array case also allocates an additional O(n) space, which dominates the O(log n) space required for the stack. So the array version is O(n), and the linked list version is O(log n).

    0 讨论(0)
  • 2021-02-01 05:37

    Mergesort is a recursive algorithm. Each recursive step puts another frame on the stack. Sorting 64 items will take one more recursive step than 32 items, and it is in fact the size of the stack that is referred to when the space requirement is said to be O(log(n)).

    0 讨论(0)
提交回复
热议问题