Complexity

Complexity is the unit of measurement of an algorithm to examine the time and space (memory) it requires to execute successfully. Complexity also measures how much more time and space are required as the input of said algorithms grows in size. An algorithm is said to be efficient and fast if it takes less time to execute and consumes less memory space.

Generally, complexity is used to compare two or more algorithms to evaluate which one will be more suitable for the task at hand.

Types of Complexity

There are six types of complexity:

  1. Constant Complexity [O(1)]
  2. Logarithmic Complexity [O(log n)]
  3. Linear Complexity [O(n)]
  4. Quadratic Complexity [O(n^2)]
  5. Factorial Complexity [O(n!)]
  6. Exponential Complexity [O(2^n)]

To measure complexity, there are two steps:

  1. Time Complexity
  2. Space Complexity