As we know that a by Designing an Algorithm, a problem can be solved. To perform some tasks based on what the problem is asking. There could be many solutions for a single problem, and each solution results in a unique or similar algorithm. So it becomes very difficult to decide which algorithm to choose from the set of algorithms so that we can get a solution in few steps or finite time. Here, the complexities of an algorithm come into the picture.
The complexity of an algorithm defines the performance of the algorithm in terms of the input size. We consider the complexities of every algorithm and compare them while choosing the most efficient algorithm to solve our problem.
There are 2 types of complexity to consider for an algorithm
- Time Complexity
- Space Complexity
It evaluates how efficiently the algorithm works as the size of input increases in comparison with its alternate algorithms/solutions. The size of input we consider here varies from 1 to millions and sometimes billions. In simple words, we can see how the algorithm behaves when the input is very low, and gradually, we increase the input size. The input size is generally based on the problem conditions and constraints. The efficiency of a general algorithm is really examined when the input size is considerably high (10^8). General algorithms are the ones that can be used in Frequent like searching or sorting algorithms.
We generally consider the algorithm, which takes a few seconds to solve the problem rather than running infinitely.
Here, the term memory is used as space. It evaluates the amount of extra memory used to reach a solution. Whenever the code is executed, some memory is provided to the text/code written, and extra memory is allocated to each variable and data structure used in the program.
For Example, if we need to add N integers and for this purpose, we are creating a variable for each integer, these N variables will have their own N memory location. The space complexity will become O(N) (read as the order of N).
While designing an algorithm, we consider minimizing the time as well as space. An algorithm is selected based on the minimal time of execution and sometimes minimal or constant space. In practice, we design an algorithm considering the time complexity and less about the space complexity.
An algorithm that takes less time and less space is considered the optimal and efficient one.