Big O notation is a way to express the upper bound of an algorithm’s running time or space complexity, as the input size approaches infinity. To calculate the Big O of an algorithm, you need to determine the number of basic operations that the algorithm performs, and how that number of operations grows as the input size increases.
Here are the steps to calculate the Big O of an algorithm:
- Identify the basic operations: These are the fundamental operations that the algorithm performs repeatedly. For example, an algorithm that sorts an array might perform basic operations such as comparing two elements and swapping them.
- Express the input size in terms of n: The input size is usually represented by the variable n. For example, if the input is an array of n elements, then n is the input size.
- Determine the number of times each basic operation is performed: This will depend on the specific algorithm and the input size. For example, if an algorithm has a nested loop, the number of times the inner loop is executed will depend on the value of the outer loop.
- Determine the worst-case scenario: The worst-case scenario is the scenario where the algorithm performs the maximum number of basic operations. It’s the scenario where the input size is at its largest.
- Express the number of basic operations in terms of n: Use mathematical notation to express the number of basic operations in terms of n. For example, if an algorithm has a nested loop and the outer loop runs n times and the inner loop runs n times, the number of basic operations is n * n or n^2.
- Simplify the expression: If the expression is a sum or product of several terms, find the term with the highest degree and drop all other terms. for example if you have 5n^2 + 3n + 2, the big O notation is O(n^2)
- Use the correct notation: The final expression will be in the form of O(f(n)) where f(n) is a function of n. f(n) describes the growth rate of the algorithm, and the notation O(f(n)) expresses the upper bound of the running time or space complexity as the input size approaches infinity.
It’s worth noting that Big O notation provides an upper bound on the performance of an algorithm and it’s possible that the actual running time of the algorithm may be faster than what is described by the Big O notation. Also, Big O notation only describes the worst-case scenario, other notations like Big Omega notation (Ω(n)) and Big Theta notation (Θ(n)) could be used to describe the best-case and average-case scenarios.
Great post!