Skip to content

Complexity

Its a way to categorize an algorithms time or memory requirements based on input. It is not supposed to be tell about an exact measurement but give an idea to generalize the growth of the algorithms based on the input and how different algorithms can be compared.

  • Growth is with respect to input: O(n) means the algorithm takes roughly n no of operation to completion.
  • Constants are dropped: O(2n) == O(n) as if n is a really large number we can say that both algorithms will supposedly grows identically.
  • Worst case scenario is ussually what is compared in algorithms cause in real world with a lot of users (ik not probable), the worst case scenario can occur a lot of times.

Various complexities

  • O(1): Constant complexity. The algorithm wouldnt grow no matter how much we increase the input
  • O(logn): Logarithmic complexity. The algorithm would grow based on base 2 logarithm of input size n.
  • O(n): Linear complexity: No of operations would grow linearly based on no of operations.
  • O(n^2): Quadratic complexity: No of operations would grow quadritically.
  • O(2^n): Exponential complexity. No of operations would grow exponentially.
  • O(n!): Factorial complexity. No of operations would grow according to factorial of n.

Big O