Space Complexity
Table of Contents + โ
In the previous section, we learned about Time Complexity. Now, we will learn about Space Complexity.
๐พ What is Space Complexity?
Space Complexity is used to measure the amount of memory an algorithm uses during its execution based on the size of the input data.
so what does this mean? For smaller input dataset, the algorithm may need less memory to execute, but as the input dataset becomes larger, the algorithm may require more memory to execute.
๐ How Space Complexity is different from Time Complexity?
Time complexity represents how the execution time of an algorithm changes with the size of the input data, while space complexity represents how the memory usage of an algorithm changes with the size of the input data.
๐ง Which memory is considered in Space Complexity?
When we talk about space complexity, we consider Main memory (RAM) used by the algorithm during its execution. Because the algorithm use RAM to store variables, data structures, and other information needed for its execution.
Note
We do not consider memory used by the operating system, compiler, or other programs running on the computer.
Lets see some common space complexities in detail!
โ๏ธ O(1) space complexity
O(1) space complexity:
- known as constant space complexity
- means that the algorithm uses a fixed amount of memory regardless of the size of the input data
๐งช Example of O(1) space complexity:
Calculate the sum of all elements in an array.
public void sumOfArray(int[] arr) { int sum = 0; // This variable takes O(1) space for (int i = 0; i < arr.length; i++) { sum += arr[i]; } System.out.println("Sum: " + sum);}In this example:
- The variable
sumalways takes same space in memory, regardless of the size of the input arrayarr. - Then we just loop through the array to calculate the sum.
- We do not store any other things in memory that depend on the size of the input array.
So, the space complexity of this algorithm is O(1).
โ๏ธ O(n) space complexity
O(n) space complexity:
- known as linear space complexity
- means that the algorithm uses an amount of memory that grows linearly with the size of the input data
๐งช Example of O(n) space complexity:
Copy all elements from one array to another array.
public int[] copyArray(int[] arr) { int n = arr.length; int[] newArr = new int[n]; // This array takes O(n) space for (int i = 0; i < n; i++) { newArr[i] = arr[i]; } return newArr;}In this example:
- The new array
newArrvariable will always take space in memory based on the size of the input arrayarr. - So, if the input array has
nelements, the new array will also havenelements. - then we have taken n variable to store the length of the array, which takes always same space in memory, regardless of the size of the input array.
- then we just loop through the array to copy the elements.
- We do not store any other things in memory that depend on the size of the input array.
So, the space complexity of this algorithm is O(n) + O(1) and we can ignore O(1) so the final space complexity is O(n).
โ๏ธ O(n^2) space complexity
O(n^2) space complexity:
- known as quadratic space complexity
- means that the algorithm uses an amount of memory that grows quadratically with the size of the input data
๐งช Example of O(n^2) space complexity:
Create a 2D array (matrix) of size n x n.
public int[][] createMatrix(int n) { int[][] matrix = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j] = i + j; } } return matrix;}In this example:
- The 2D array
matrixvariable will always take space in memory based on the size of the inputn. - So, if
nis the size of one dimension, the total number of elements in the 2D array will ben * n, which isn^2. - then we just loop through the array to fill the elements.
- We do not store any other things in memory that depend on the size of the input
n.
So, the space complexity of this algorithm is O(n^2).
โ๏ธ O(log n) space complexity
O(log n) space complexity:
- known as logarithmic space complexity
- means that the algorithm uses an amount of memory half or reduces with the size of the input data
๐งช Example of O(log n) space complexity:
Recursive function to calculate the power of a number.
function power(x, n) { if (n === 0) return 1; const half = power(x, Math.floor(n / 2)); if (n % 2 === 0) return half * half; return half * half * x;}In this example:
- The function
poweruses recursion to calculate the power of a number. - Each recursive call reduces the size of the input
nby half. - The space used by the call stack is proportional to the logarithm of the input size.
So, the space complexity of this algorithm is O(log n).
๐ Space complexity in increasing order
Here are some common space complexities we may see. These are increasing in order means the first one is the best and the last one is the worst.
| Space Complexity | Name | Description |
|---|---|---|
O(1) | Constant Space | algorithm uses a fixed amount of memory regardless of the input size |
O(log n) | Logarithmic Space | algorithm uses an amount of memory that grows logarithmically with the input size |
O(n) | Linear Space | algorithm uses an amount of memory that grows linearly with the input size |
O(n log n) | Linearithmic Space | algorithm uses an amount of memory that grows in proportion to n log n with the input size |
O(n^2) | Quadratic Space | algorithm uses an amount of memory that grows quadratically with the input size |
O(2^n) | Exponential Space | algorithm uses an amount of memory that grows exponentially with the input size |
โ Conclusion
In this section, we learned about Space Complexity in detail. We learned what is space complexity, how it is different from time complexity, which memory is considered in space complexity and how to calculate space complexity with examples of O(1), O(n), O(log n), O(n^2) and more.