Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
big o notation examples

Big o notation examples with solutions

Posted on 12 May 202011 April 2023 By YASH PAL No Comments on Big o notation examples with solutions

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 in algorithms analysis

Big O notation examples

Big o asymptotic 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)

Big o asymptotic notation examples

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).

Big o asymptotic notation examples

In the above example, the algorithm has only one input output and a loop. the loop has 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 n2

so the function f(n) is O(n2)

Big o asymptotic notation examples

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 + n2

so the f(n) is O(n2)

Big o asymptotic notation examples

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 n3 + n2

so the algorithm is the order of O(n3)

Big o asymptotic notation examples

In the above example algorithm, we have one nested for loop so in simple terms the algorithm has an order of n2

Big o asymptotic notation examples

this is also the order of n2

Big o asymptotic notation examples

this is also the order of n2

Big o asymptotic notation examples

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).

Big o asymptotic notation examples

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 + n2

so the algorithm has the order of O(n2)

Big o asymptotic notation examples

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.

Algorithms Tutorials, Computer Science Tutorials Tags:computer science, Data Structure

Post navigation

Previous Post: Operations on the linked list
Next Post: Insertion in Linked list | Data structures

Related Tutorials

vi editing commands VI Editing Commands For Linux Computer Science Tutorials
modes of vi editor VI Editor in Linux Computer Science Tutorials
environment and path setting in linux Environment and Path Setting in Linux Computer Science Tutorials
hard and synbolic links in linux Hard links and Symbolic links Computer Science Tutorials
changing file access permissions Changing File Access Permissions in Linux Computer Science Tutorials
access permissions in linux Access permissions in Linux Computer Science Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick your Subject

  • Internet of Things
  • Data Structures/Algorithms
  • Interview Preparation
  • Human Values
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

  • Algorithms Tutorials
  • Basic Programming
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Linux
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme