Dsa TimeComp
Dsa TimeComp
Space Complexity
You now understand space and time complexity fundamentals and how to calculate it
for an algorithm or program. In this section, you will summarize all previous discussions
and list the key differences in a table.
The size of the input data is the primary Primarily determined by the
determinant. auxiliary variable size
Big O Notation:
Time complexity is frequently expressed using Big O notation. It represents the
maximum possible running time for an algorithm given the size of the input. Let's go
through some crucial notations.:
int low = 0;
int high = size - 1;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return sum;
}
The printArray function has a time complexity of O(n) as it iterates over each element in
the array to print its value.
Advertisement
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
The Fibonacci function has a time complexity of O(2^n) as it recursively calculates the
Fibonacci sequence, resulting in an exponential increase in the execution time as the
input size increases.
f) O(n!) - Factorial Time Complexity:
An algorithm whose runtime increases proportionally to the size of the input. This kind
of time complexity is frequently seen in algorithms that generate every combination
or permutation of a set of components.
Arrays:
Access: O(1)
Search: O(n)
Insertion (at the end): O(1)
Insertion (at the beginning or middle): O(n)
Deletion (from the end): O(1)
Deletion (from the beginning or middle): O(n)
Linked Lists:
Access: O(n)
Search: O(n)
Insertion (at the beginning): O(1)
Insertion (at the end, with a tail pointer): O(1)
Insertion (at the end, without a tail pointer): O(n)
Insertion (in the middle): O(n)
Deletion (from the beginning): O(1)
Deletion (from the end, with a tail pointer): O(1)
Deletion (from the end, without a tail pointer): O(n)
Deletion (from the middle): O(n)
Stacks:
Push: O(1)
Pop: O(1)
Peek: O(1)
Queues:
Enqueue: O(1)
Dequeue: O(1)
Peek: O(1)
Hash Tables:
Search: O(1) - on average, assuming a good hash function and minimal collisions
Insertion: O(1) - on average, assuming a good hash function and minimal
collisions
Deletion: O(1) - on average, assuming a good hash function and minimal
collisions
B-Tree:
Searching for an element: O(log n)
Insertion of an element: O(log n)
Deletion of an element: O(log n)
Red-Black Tree:
Searching for an element: O(log n)
Insertion of an element: O(log n)
Deletion of an element: O(log n)
Space complexity
Now let’s understand with an example that how to calculate the space complexity of an
algorithm.
So in the above example, there are 4 integer variables those are a, x, y, z so they will take
4 bytes(as given in the table above) space for each variable, and extra 4-byte space will
also be added to the total space complexity for the return value that is a.
Hence, the total space complexity = 4*4 + 4 = 20 bytes
But for this example, this is the fixed complexity and because of the same variables
inputs, such space complexities are considered as constant space complexities or so-
called O(1) space complexity.
So here this time there is an algorithm to find the factorial of the number using a
recursive method. Now,
1. "N" is an integer variable that stores the value for which we have to find the
factorial, so no matter what value will, it will just take "4 bytes" of space.
2. Now function call, "if" condition, "else" condition, and return function all come
under the auxiliary space, and let's assume these all will take combined “4 bytes”
of space but the matter of fact here is that here we are calling that function
recursively "N" times so here the complexity of auxiliary space will be "4*N bytes"
where N is the number of which factorial have to be found.
Hence, Total Space Complexity = (4 + 4*N) bytes But these 4 bytes are constant so we
will not consider it and after removing all the constants(4 from 4*N) we can finally say
that this algo have a complexity of "O(N)".
int 4
float 4
double 8
char 1
short int 2
long int 4
return 0;
}
Copy code
To calculate the complexity of this algorithm, we need to determine the amount of
memory used by each of the variables. In this case:
● a is an integer, which takes up 4 bytes of memory.
● b is a float, which takes up 4 bytes of memory.
● c is a character, which takes up 1 byte of memory.
● d is an array of 10 integers, which takes up 40 bytes of memory (10 x 4).
In this case, the function factorial is recursive, so it makes multiple function calls and
uses memory on the function call stack. The complexity of this algorithm is proportional
to the number of function calls, which is directly proportional to the value of n. The
more calls, the more memory will be used on the function call stack.
In the worst-case scenario, where n is very large, this algorithm can use a significant
amount of memory on the function call stack, leading to a high space-complexity.