In computer science, there are multiple ways to solve a problem, but to pick an efficient one, there are certain parameters which include time complexity and space complexity.
The space complexity is the measurement of total space required by an algorithm to execute properly. It also includes memory required by input variables. Basically, it's the sum of
Note: Space complexity
Auxiliary space Memory used by input variables
Space complexity is the parallel concept to time complexity. For example, if we want to initialize an array of
If our algorithm takes a lot of memory to execute, then our compiler will not allow us to run it. To avoid compiler crashes, we need to compute the space complexity of our algorithm. There are many notations available to compute space complexity:
Let's understand them briefly. But our focus would be on big-O notation, as it's widely used by developers:
This big-O notation describes the asymptotic upper bound, the worst case of space complexity. Basically, it's the measure of the maximum amount of space our algorithm takes to grow with respect to the size of input data. The important big-O notations are as follows:
When the algorithm takes the same amount of space regardless of the input size
When the algorithm takes space proportional to the
When an algorithm takes space directly proportional to the input size
When the space complexity of an algorithm grows proportionally to the input size
When the space complexity grows proportionally to the square of the input size
The omega notation describes the asymptotic lower bound. It is the best-case scenario in terms of time and space complexity. Actually, it's the minimum amount of space our algorithm takes to grow with respect to the input data.
The theta notation means that the space complexity of an algorithm is between its upper-bound and lower-bound space complexity.
Most developers use big-O notation because it's relatively easy to estimate the maximum amount of space required. So we'll learn to measure the upper-bound of an algorithm in this section. Let's take an example of a function that returns the sum of the two variables.
#include <iostream>using namespace std;int sum(int a , int b) // Inter a and b{return a + b; // returning sum is integer too}int main() {// your code goes hereint x = 10;int y = 20;cout << "Sum of a & b =" << sum(x,y);return 0;}
In this program, there are five variables allocated in the memory:
x
, y
.a
, b.
. sum
.A single variable uses
int sum(int arr[ ], int n) // n is the size of array arr{int arr_sum =0; // x takes 4 bits as integer variablefor (int i=0; i<n; i++) // i takes 4 bits as integer variable{arr_sum = arr_sum + arr[i];}return x;}
In this program, there are five variables allocated in the memory:
arr[]
is integer type with size int n
, int i
and int sum
will take The total memory this program takes (
Type | Size(bytes) |
bool, char, unsigned char, signed char, __int8 | 1 |
__int16, short, unsigned short, wchar_t, __wchar_t | 2 |
float, _int32, int, unsigned int, long, unsigned long | 4 |
double, __int64, long double, long long | 8 |
Free Resources