Big O
Big O is a notation which explains the performance or complexity of an algorithm .It describes the upper bound of the complexity. Let’s understand this with an example. Suppose we have two codes, code A and code B . Code A has 12 operations and Code B has 6 operations. In all scenarios, the performance of Code B will be better than Code A due to lesser number of operations.
Below are few examples with complexity details
Example Number 1
Here the loop runs for 4 times, the number of operations and inputs are 4. If there are 4 inputs, we are doing 4 comparisons. So, the complexity is linear, O(n) .
Example Number 2
The above example complexity is O(1) as it runs on constant time and number of operation is always 1 . It does not depend on number inputs.
Example Number 3
The complexity of each line is mentioned in the comment. This example takes O(n) as O(2n+5) is considered as O(n).
This is the basic description of Big O notation. Hope you find it useful!