Trusted answers to developer questions

Time complexity vs. space complexity

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

There are multiple ways to design an algorithm, or considering which one to implement in an application. When thinking through this, it’s crucial to consider the algorithm’s time complexity and space complexity.

Time complexity

The time complexity of an algorithm is the amount of time taken by the algorithm to complete its process as a function of its input length, n. The time complexity of an algorithm is commonly expressed using asymptotic notations:

  • Big O - OO(n),
  • Big Theta - Θ\Theta(n)
  • Big Omega - Ω\Omega(n)
svg viewer
svg viewer

Space complexity

The space complexity of an algorithm is the amount of space (or memory) taken by the algorithm to run as a function of its input length, n. Space complexity includes both auxiliary space and space used by the input.

Auxiliary space is the temporary or extra space used by the algorithm while it is being executed. Space complexity of an algorithm is commonly expressed using Big O (O(n))(O(n)) notation.

Many algorithms have inputs that can vary in size, e.g., an array. In such cases, the space complexity will depend on the size of the input and hence, cannot be less that O(n)O(n) for an input of size nn. For fixed-size inputs, the complexity will be a constant O(1)O(1).

It’s valuable for a programmer to learn how to compare performances of different algorithms and choose the best time-space complexity to solve a particular problem in the most efficient way possible.

RELATED TAGS

asymptotic notation
big o
time complexity
space complexity
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?