There exist numerous algorithms for solving a specific problem. We require a methodology for comparing and determining the optimal algorithm. This methodology should be independent of the underlying machine architecture and directly correlate with the input size. Therefore, we have two methods at our disposal to differentiate and evaluate a particular algorithm:
1) Time Complexity
2) Space Complexity
Time complexity-:
It is important to recognize that time complexity is not equivalent to the actual execution time of the code on a machine. Instead, it refers to the amount of time an algorithm takes to execute as a function of the input size. For instance, consider two machines: an old one and a new one. If we run an algorithm on these machines and the old one takes 10 seconds while the new one takes only 1 second, it does not necessarily imply that their time complexities are different. Time complexity focuses on understanding the growth pattern of a graph as the input size increases, reflecting how the graph behaves over time.
Space Complexity-:
When tackling a problem, a machine requires memory to store temporary data or the final result during the execution process. The amount of input memory and auxiliary memory needed by an algorithm to solve a problem is referred to as space complexity.