Skip to content
Learnearn.uk » IB Computer Science » Big O Notation

Big O Notation

What is Big O Notation?

What is Big O Notation?

Big O Notation is a mathematical concept used in computer science to describe the **performance or complexity** of an algorithm. It expresses **how the execution time or memory usage grows** relative to the size of the input.

Big O focuses on the **worst-case scenario**, helping developers ensure algorithms will perform reliably even with large data sets.

Why It Matters

Understanding Big O is crucial for writing efficient code. Here’s why:

Efficiency: Better algorithms lead to faster, more efficient programs.
Scalability: Big O helps predict how code behaves with growing input sizes.
Optimization: Identifies bottlenecks in time and space usage.
Technical Interviews: A key topic in coding interviews and assessments.

Common Notations

Common Big O Notations

Big O Name Example
O(1) Constant Time Accessing an element in an array
O(log n) Logarithmic Time Binary search
O(n) Linear Time Looping through an array
O(n log n) Linearithmic Time Merge sort, quicksort
O(n²) Quadratic Time Nested loops (e.g., bubble sort)
O(2ⁿ) Exponential Time Recursive algorithms (e.g., Fibonacci)
O(n!) Factorial Time Brute-force permutations

 

Examples

Examples

1. O(1) – Constant Time

def get_first_element(arr):
    return arr[0]
    This function returns the first element regardless of input size.

2. O(n) – Linear Time

def print_all_elements(arr):
    for item in arr:
         print(item)

The number of operations grows linearly with the input.

3. O(n²) – Quadratic Time

def print_pairs(arr):
    for i in arr:
        for j in arr:
            print(i, j)

Nested loops cause the time to grow quadratically

Space Complexity

Space Complexity

Big O is also used to describe **space complexity**, or how much memory an algorithm uses.
O(1): Constant space, memory usage doesn’t grow with input.

O(n): Linear space, memory usage grows with input size (e.g., storing results in a new array).

O(n²): Common in matrix-based algorithms or recursive calls with large depth.

Efficient memory usage is just as important as fast execution in large-scale systems

Tips for Analyzing Code

Tips for Analyzing Code

– Ignore constants: O(2n) simplifies to O(n)

– Drop smaller terms: O(n² + n) simplifies to O(n²)

– One loop = O(n), nested loops = O(n²), triple loops = O(n³)

– Recursive functions often follow patterns like T(n) = T(n/2) + O(1)

 

Graph