Skip to content

Commit ebfc011

Browse files
authored
Merge pull request #58 from ParnaviKulkarni/parnabranch
Added Information to space-complexity.md
2 parents c707c0a + 1b0642f commit ebfc011

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

lessons/space-complexity.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,66 @@ order: "7C"
55
section: "Space & Time Complexity"
66
description: "learn about time and space complexity for various algorithms"
77
---
8+
9+
Now that we have learned about 'time' aspect of performance analysis of an algorithm, let's move on to memory aspect of the same.
10+
11+
***Space complexity of an algorithm is basically the amount of memory it needs to run to completion.***
12+
13+
It is a way to establish approximate relationship between size of input data and primary memory used by the algorithm to produce the expected result.
14+
15+
Calculation of space complexity used to hold much more significance in early days of computing than it does now. This is because most machines today have large memories and the user does not need to worry about running out of memory for running a program or two. But it is a crucial estimate where the physical memory is limited or closely monitored.
16+
17+
## **Calculation of Space Complexity**
18+
19+
An algorithm's space can be categorized into 2 parts:\
20+
**1)Fixed Part** which is independent of characteristics of input and output.\
21+
It includes instruction(code) space, space for simple variables, fixed-size component variables and constants.\
22+
**2)Variable Part** which depends on instance characteristics.\
23+
It consists of the space needed by component variables whose size is dependent on the particular problem instance being solved, the space needed by referenced variables, and the recursion stack space.
24+
25+
Thus, space requirement S(P) of any algorithm P is:
26+
> S(P) = c + Sp (Instance characteristics), where c is constant
27+
28+
While analyzing space complexity, we primarily concentrate on estimating Sp.
29+
Consider following algorithm:
30+
```
31+
Algorithm Calc(a, b, c)
32+
{
33+
return a + b - c*a + b/c - a;
34+
}
35+
```
36+
Here, problem instance depends only upon *a*,*b* and *c* which can be considered constant(fixed part). Thus ***Sp=0***. So, the space required by *Calc* algorithm is constant. In other words, complexity is O(1).
37+
38+
When the program consists of loops (In case of Iterative algorithms), it will have linear space complexity or O(n).
39+
While dealing with operations on data structures, we can say that space complexity depends on size of the data structure. For example, if an array stores N elements, its space complexity is O(n). A program with an array of N arrays will have space complexity O(n^2) and so on.
40+
41+
Space complexity analysis also takes into account the size of recursion stack in case of recursive algorithms. for example,
42+
```
43+
Algorithm Fact(n)
44+
{
45+
if (n<=0) return 1;
46+
else return n * (n - 1);
47+
}
48+
```
49+
In this case there are 3 statements (1 if & 2 return statements). The depth of recursion is *n + 1*. Thus the recursion stack space needed is >=3(n+1). So we can say, space complexity is O(n) i.e. linear.
50+
51+
***NOTE: Space complexity might differ from machine to machine based on the programming language, the compiler etc.***
52+
53+
54+
## **Space Complexities of Common Algorithms**
55+
| Algorithm | Space Complexity |
56+
| --------- | ---------------- |
57+
| Linear Search | O(1) |
58+
| Binary Search | O(1) |
59+
| Bubble Sort | O(1) |
60+
| Insertion Sort | O(1) |
61+
| Selection Sort | O(1) |
62+
| Heapsort | O(1) |
63+
| Shell Sort | O(1) |
64+
| Quicksort | O(log(n)) |
65+
| Mergesort | O(n) |
66+
| Timsort | O(n) |
67+
| Tree Sort | O(n) |
68+
| Bucket Sort | O(n) |
69+
| Radix Sort | O(n+k) |
70+
| Counting Sort | O(k) |

0 commit comments

Comments
 (0)