Learnmonkey Learnmonkey Logo

Big O Notation

In this tutorial, we'll learn about Big O Notation, one the fundamental concepts with Data Structures And Algorithms.

What is Big O Notation?

Big O Notation is a programming and mathematical concept which measures time and space complexity. Time complexity is the amount of time a function runs, and space complexity is the amount of space, or memory, a function takes up. However, the most used purpose of Big O Notation is for time complexity.

Time Complexity

Big O Notation doesn't measure the amount of time a function takes to run in seconds are actual time, but in measures of "O". For example, we could write "O(3)", meanning that the code runs a linear expression or code 3 times. It's okay if you don't understand right now, but at the end of this tutorial, you'll get the idea.

Measuring Time Complexity Examples

Here are some examples of measuring Time Complexity with Big O Notation:

Example #1


for i in range(5):
    print(i)
        
In this example, the code runs five times, and can be measured at O(5).

Example #2


for i in range(5):
    for j in range(5):
        print(i + j)
        
In this example, the code runs five times for five times, or 5 x 5 times (25), so it can be measured at O(25).

Example #3


print("[insert text here :)]")
        
In this example, we run what is called a linear function, or a function at O(1).