Algorithms
- A recipe for a computer to follow
- Has an input and an output
- Algorithms need to be deterministic so that the output is consistent and not stochastic
Time Complexity
- The amount of the time the algorithm needs to run relative to the input size
- As the input size grows, how much longer does the algorithm need to complete
Space Complexity
- The amount of memory the algorithm needs relative to the input size
- As the input size grows, how much more memory does the algorithm take
Calculating the complexity
- In the context of time, you are calculating the number of operations the algorithm performs relative to the input size
- In the context of space, you are calculating the amount of memory the algorithm takes relative to the input size
Example
nums = [array]
max_num = 0
for num in nums:
if num > max_num:
max_num = num