这个递归插入排序算法的空间复杂度是多少?

huangapple go评论67阅读模式
英文:

What is the space complexity of this recursive Insertion Sort algorithm?

问题

The space complexity for the provided code is O(n) because of the recursive call stack. Each recursive call to recursiveInsertionSort pushes a new frame onto the call stack, and this continues until the base case (n <= 1) is reached.

In each frame, variables like val and j are allocated, and the function's state is saved on the stack. Since there can be up to n recursive calls in the worst case (when the input array is sorted in reverse order), you will have n frames on the call stack simultaneously, leading to a space complexity of O(n).

The fact that you are passing the array by reference does not affect the space complexity because the space required for the call stack is determined by the number of recursive calls, not the size of the array itself.

英文:
void recursiveInsertionSort(vector&lt;int&gt; &amp;arr, int n) {
    if (n &lt;= 1)
        return;
    recursiveInsertionSort(arr, n - 1);
    int val = arr[n - 1], j = n - 2;
    for (j = n - 2; j &gt;= 0 &amp;&amp; arr[j] &gt; val; --j)
        arr[j + 1] = arr[j];
    arr[j + 1] = val;
}

I thought the space complexity is constant (O(1)),
because I am passing the array by reference.

But I was told it is actually O(n). Why is that ?

答案1

得分: 6

The data vector is still occupying space somewhere, even if your function gets it by reference.

但即使您不希望考虑这个空间,您的代码仍然需要O(n)的空间。

这是因为您将有O(n)个递归调用,每个调用在调用栈上占用(在您的情况下) O(1) 的空间(用于存储调用上下文 - 参数等)。当您在最内部的递归调用中时,所有这些空间都将需要。

总共是O(n)的空间


注意:
在这种情况下,val实际上每次只会有一个实例存在,因为它是在递归调用之后分配的。
但即使在调用之前定义和使用它,每次调用仍然只会占用O(1)的空间,最终结果将是相同的。

英文:

The data vector is still occupying space somewhere, even if your function gets it by reference.

But even if you do not want to consider this space, your code still requires O(n) space.

This is because you'll have O(n) recursive calls, each occupying (in your case) O(1) space on the call stack (for storing the call context - arguments etc.). All this space will be needed when you'll be in the innermost recursive call.

Altogether it is O(n) space.


Note:
val will actually have only 1 instance alive at any given time in this case, because it is allocated after the recursive call.
But even if it was defined and used before the call, it would still be O(1) space per call, and the final result would be the same.

答案2

得分: 2

这是因为在每次递归调用中,您占用O(1)空间,而函数调用总共进行了N次,因为数组的大小是N,所以值占用了O(1) * N的空间,从而导致了算法的整体O(N)空间复杂度。

O(1) * N = O(N)
英文:

It is consuming O(N) linear space complexity because in each recursive call you are O(1) space, and the function call is made N times as the size of the array, so the values are occupying O(1) * N spaces, which leads to the overall O(N) space complexity of the algorithm.

O(1) * N = O(N)

答案3

得分: 1

O(n) 在这里表示算法使用与输入数据大小相同顺序的空间。这正是你所说的:它使用输入数据大小的一倍。(不包括使用的堆栈空间)

即使你只复制了数组一次,它仍然是O(n),因为O(2*n) 约等于 O(n)。如果你按值传递数组,那么空间使用可能会上升,可能到O(n^2)。

英文:

O(n) here means that the algorithm uses space in the order of the input data size. Which is exactly what you say: It uses one time the size of the input data. (not counting the stack space used, though)

Even if you took a sinle copy of the array, it would still be O(n), since O(2*n) ~ O(n). If you passed the array by value, then the space usage would rise, probably to O(n^2).

huangapple
  • 本文由 发表于 2023年4月17日 15:03:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定