Should the spatial complexity of an insertion sort be O (N)?

This covers the "software algorithm" from https://stackoverflow.com/help/on-topic

This is from a class lecture slide enter image description here

Here is the insertion sort implementation we used

public static void insertionSort(int[] a) {
     for (int i = 1; i < a.length; i++) {
          int temp = a[i];
          int j = i;
          while (j >= 1 && a[j - 1] > temp) {
                a[j] = a[j - 1];
         }
         a[j] = temp;
     }
}

I would agree that the spatial complexity of the local variables will be O (1), because every time the size of the input file, i, j and temp will be processed only by the same local variable, each will occupy a block of memory.

However, I am confused by the spatial complexity of the array. http://www.cs.northwestern.edu/academics/courses/311/html/space-complexity.html had a similar example,

int sum(int a[], int n) {
    int r = 0;
    for (int i = 0; i < n; ++i) {
       r += a[i];
    }
    return r;
}

, N a, O (N)?

? O (N), N ( ) O (1) - ?

+4
3

Array passed by reference , , .

, (, (a)), O(1), O(n).

O(1) , . , , :

. , . A B B, .

- , , , (b).


(a) , O(1) .


(b) , , , , , . , , , , , " - ", , , :

. .

. , , , , , , . , - .

, , Google, .

, , , .

, , -, , . , ( ), .

, , , , , .

, , , , , : -)

, , , , - . , , .

+5

.

- :

A M ( ) B, N , A M + N . , A B 3 ? , , , A B 3 , M + N.

A , , B , A. , , , , .

, A, B ( sum) . sum , , , sum.

-1

, - . , , , , .

, .

-1

All Articles