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

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

提交回复
热议问题