Big O Notation in CIn Data Structure and Algorithms in C programming, we have learned so many algorithms where we have understood different aspects and purposes of an algorithm. We have also studied the complexity of an algorithm and how to analyze and calculate an algorithm's complexity. We have found the time and space complexity of an algorithm and concluded that the algorithm that has less time and space complexity is evaluated as the best algorithm. We understood how to find the best case, worst case, and the average case of an algorithm. Thus, for analyzing all such complexities and representing them, the concept of Asymptotic Notation is used under which there are different types available for representing the complexities. One such type is Big O Notation. In this section, we will discuss the Big O notations and briefly introduce Asymptotic notations and its types. What are Asymptotic NotationsThese are the mathematical notations that are used for the asymptotic analysis of the algorithms. The term 'asymptotic' describes an expression where a variable exists whose value tends to infinity. In short, it is a method that describes the limiting behavior of an expression. Thus, using asymptotic notations, we analyze the complexities of an algorithm and its performance. Using the asymptotic notations, we determine and show the complexities after analyzing it. Therefore, there are three types of asymptotic notations through which we can analyze the complexities of the algorithms:
So, these three asymptotic notations are the most used notations, but other than these, there are more common asymptotic notations also present, such as linear, logarithmic, cubic, and many more. Big O NotationThe Big O notation is used to express the upper bound of the runtime of an algorithm and thus measure the worstcase time complexity of an algorithm. It analyses and calculates the time and amount of memory required for the execution of an algorithm for an input value. Mathematically, For a function, f(n) and another function g(n), where both functions are defined on some unbounded set of real (positive) numbers. Where g(n) is strictly positive for all large values of n. It can be written as: f(n) = O(g(n)) where n tends to infinity (n → ∞) But it is seen that the assumption of n to infinity is left unstated, and so we can simply write the above expression as: f(n) = O(g(n)) Here, f and g are the necessary functions from positive integer to nonnegative real numbers. Thus, the Big O asymptotic refers to large n values. Properties of Big O NotationCertain essential properties of Big O Notation are discussed below:
Here, in terms of Big O, every log functions increase in the same manner. How does Big O Notation make runtime analysis of an algorithmFor analyzing an algorithm's performance, we used to calculate and compare the worstcase running time complexities of the algorithm. The order of O(1), which is known as the Constant Running Time, is considered to be the fastest running time for an algorithm where the time taken by the algorithm is the same for different input sizes. However, the constant running time is the ideal runtime for an algorithm, but it is achieved very rarely. It is because the runtime of an algorithm depends on the input size of n. For example: As we know that the runtime performance of an algorithm depends on the input size of n. Let's see some mathematical examples for making the runtime analysis of an algorithm for different size of n:
Thus, similarly, we calculate the runtime performance of an algorithm. Let's see some algorithmic examples and see the runtime analysis of those algorithms:
How does Big O notation analyze the Space complexityIt is essential to determine both runtime and space complexity for an algorithm. It's because on analyzing the runtime performance of the algorithm, we get to know the execution time the algorithm is taking, and on analyzing the space complexity of the algorithm, we get to know the memory space the algorithm is occupying. Thus, for measuring the space complexity of an algorithm, it is required to compare the worstcase space complexities performance of the algorithm. In order to determine the space complexity of an algorithm, the following two tasks are necessary to be done: Task 1: Implementation of the program for a particular algorithm is required. Task 2: The size of the input n is required to know the memory each item will hold. Both these are two important tasks to be accomplished first then only we can calculate the space complexity for an algorithm. Examples of AlgorithmsBelow we have mentioned some algorithmic examples with their space complexities:
Example of Big O Notation in CBelow we have implemented the selection sort algorithm in C and calculated the worstcase complexity (Big O notation) of the algorithm: In order to analyze the algorithm:
You can implement other algorithms in C, analyze it and determine the complexities in a similar way.
Next TopicLCM of two numbers in C
