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

前端 未结 2 1475
傲寒
傲寒 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).

提交回复
热议问题