**Big o**is also called an asymptotic analysis of the algorithm. it defines the complexity of an algorithm on different inputs size and finds the order of the algorithm using the function.

so today we are going to learn how to calculate big o notation over the for loops, functions, and operators using the different types of examples.

there are some rules to find the big o notation of an algorithm. so if you don't know these rules then you should read this post first.

###
__Big O notation examples__

in the above example, we have one assignment operator and three for loops. but one for loop is only executed 6 times. so the running time of the third loop is constant so we don't consider that.

next first for loop maximum executes for n-2 times and the second for loop executes for n times.

so the running time of the algorithm is n + n -2

so the algorithm is an order of n.

f(n) is O(n)

in the above example, the algorithm has only one for loop and one input and output. but the inputs and outputs execute in constant time so we don't consider them.

next, the for loop is maximum executes for n times. so the complexity of the algorithm is n. and its order is n.

f(n) is O(n).

In the above example, the algorithm has only one input-output and a loop. the loop is executed a maximum of n+1 times. so the running time of the algorithm is n+1 which is nothing but the order of n.

so f(n) is O(n).

In the above example, we have a nested for loop means in a loop we have another for a loop. and both algorithms can run a maximum of n times. so we can say the complexity of an algorithm is n*n that is nothing but n

^{2}
so the function f(n) is O(n

^{2})
next in the above example algorithm we have a nested for loop and in the for loop we have an assignment operator that maximum executes for n times.

and for the operator, the running time is the order of logn. but we also have nested for loops and both can run maximum for n times. so the complexity of the algorithms is nlogn + n

^{2}
so the f(n) is O(n

^{2})
in the above example algorithm, we have two nested for loops. and in the first nested for loop, we have three two inners for loop and in the second we have one inner for loop. so the complexity of the algorithm is n

^{3}+ n^{2}
so the algorithm is the order of O(n

^{3})
In the above example algorithm, we have one nested for loop so in simple terms the algorithm has an order of n

^{2}
this is also the order of n

^{2}
this is also the order of n

^{2}
in the above example, we have a loop that can run a maximum of n times but we also have a function in the loop that can also run a maximum of n times.

so every function has the complexity of logn. so we can say that the complexity of the algorithm is nlogn.

so the function has an order of O(nlogn).

in the above example, we have a for loop and an if-else statement. and in any conditional statement, we select the complexity of that condition that can run maximum time. so in the if-else statement if the condition can run maximum time so we consider that. and for loop can run maximum n times. also, we have an assignment operator in the loop that can run a maximum of n times. so the complexity of the algorithm is nlogn + n

^{2}
so the algorithm has the order of O(n

^{2})
in the above example, we have a loop that can run a maximum of 5 times so the whole time to run this algorithm is constant. so it's an order O(1) algorithm.

Also, read other tutorials as well

- What are Data Structures and algorithms
- Algorithm design and analysis
- Classification of algorithms
- How to calculate the running time of an algorithm.
- Worst Average and Best-case analysis of the algorithm.
- Linked List in Data Structures
- Traversing in Linked list
- Operations on the linked list
- Insertion in the linked list
- Deletion in a linked list
- Reversing a linked list
- Sorting a linked list
- Find and remove the loop in the linked list
- Doubly linked list
- Insertion in the doubly linked list
- Deletion in the doubly linked list
- Reversing a doubly linked list
- Circular linked list
- Insertion in the circular linked list
- Deletion in the circular linked list
- Merge two linked list
- Header linked list
- Sorted linked list
- Stack in data structures
- Queue in data structures
- Circular queue
- Dequeue in the data structure
- Priority queue
- Polish notation
- Tree in the data structure
- Binary tree
- Array representation of the binary tree
- linked representation of a binary tree
- Traversing in the binary tree
- Inorder traversal in the binary tree
- Preorder traversal in the binary tree
- Postorder traversal in the binary tree
- Level order traversal in the binary tree
- Binary search tree
- Insertion in the binary search tree
- Deletion in the binary search tree
- Heap in data structures

## 1 Comments

i am yash

ReplyDelete