Asymptotic Notation

Big O, Omega, Theta — analyzing algorithm complexity

Introduction

Asymptotic Notation is a mathematical technique used to analyze and compare the performance of algorithms as the input size grows.

Instead of measuring execution time in seconds (which varies based on hardware, CPU speed, memory, and operating system), we measure how the running time increases relative to the input size n.

This allows us to compare algorithms independently of the machine on which they run.


Why Do We Need Asymptotic Analysis?

Consider two algorithms.

Algorithm A

for(int i = 0; i < n; i++) {
    System.out.println(i);
}

Algorithm B

for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
        System.out.println(i + " " + j);
    }
}

For:

n = 10

Algorithm A performs approximately:

10 operations

Algorithm B performs approximately:

100 operations

For:

n = 1000

Algorithm A performs:

1000 operations

Algorithm B performs:

1,000,000 operations

As the input size grows, Algorithm B becomes significantly slower.

Asymptotic analysis helps us understand this growth.


Key Concepts

Input Size (n)

The amount of data processed by an algorithm.

Example:

int[] arr = new int[100];

Here:

n = 100

Time Complexity

Time Complexity measures how the execution time of an algorithm grows with input size.

Example:

for(int i = 0; i < n; i++) {
    System.out.println(i);
}

The loop runs n times.

Time Complexity:

O(n)

Space Complexity

Space Complexity measures the amount of memory used by an algorithm.

Example:

int[] arr = new int[n];

Memory required:

n elements

Space Complexity:

O(n)

Types of Asymptotic Notation

There are three major asymptotic notations:

  1. Big O Notation (O)
  2. Omega Notation (Ω)
  3. Theta Notation (Θ)

1. Big O Notation (Upper Bound)

Big O represents the worst-case complexity of an algorithm.

It answers the question:

> What is the maximum amount of work the algorithm may perform?


public static int search(int[] arr, int target) {

    for(int i = 0; i < arr.length; i++) {
        if(arr[i] == target)
            return i;
    }

    return -1;
}

Best Case

Target is found at the first position.

1 comparison

Worst Case

Target is found at the last position or not found.

n comparisons

Big O Complexity:

O(n)

Example: Single Loop

for(int i = 0; i < n; i++) {
    System.out.println(i);
}

Operations:

n

Complexity:

O(n)

Example: Nested Loop

for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
        System.out.println(i + j);
    }
}

Operations:

n × n = n²

Complexity:

O(n²)

2. Omega Notation (Lower Bound)

Omega represents the best-case complexity.

It answers:

> What is the minimum amount of work the algorithm must perform?


Array:

[10, 20, 30, 40]

Searching for:

10

Found immediately.

Operations:

1

Omega Complexity:

Ω(1)

3. Theta Notation (Tight Bound)

Theta represents both the upper and lower bounds.

It gives the exact growth rate of an algorithm.


Example

for(int i = 0; i < n; i++) {
    System.out.println(i);
}

Best Case:

n

Worst Case:

n

Theta Complexity:

Θ(n)

Comparison of O, Ω and Θ

NotationMeaning
O(f(n))Upper Bound (Worst Case)
Ω(f(n))Lower Bound (Best Case)
Θ(f(n))Tight Bound (Exact Growth)

Linear Search Example

CaseComplexity
Best CaseΩ(1)
Average CaseΘ(n)
Worst CaseO(n)

Common Time Complexities


O(1) - Constant Time

Execution time remains constant regardless of input size.

Example:

int first = arr[0];

Complexity:

O(1)

Characteristics

  • Fastest complexity
  • Independent of input size

O(log n) - Logarithmic Time

Input size is repeatedly divided.

Most common example:

Binary Search


Binary Search Example

public static int binarySearch(int[] arr, int target) {

    int left = 0;
    int right = arr.length - 1;

    while(left <= right) {

        int mid = (left + right) / 2;

        if(arr[mid] == target)
            return mid;

        if(arr[mid] < target)
            left = mid + 1;
        else
            right = mid - 1;
    }

    return -1;
}

For:

n = 1024

Search process:

1024
512
256
128
64
32
16
8
4
2
1

Only about:

10 comparisons

Complexity:

O(log n)

O(n) - Linear Time

Work increases proportionally with input size.

Example:

for(int i = 0; i < n; i++) {
    System.out.println(arr[i]);
}

Complexity:

O(n)

O(n log n) - Linearithmic Time

Common in efficient sorting algorithms.

Examples:

  • Merge Sort
  • Heap Sort
  • Quick Sort (Average Case)

Complexity:

O(n log n)

O(n²) - Quadratic Time

Usually occurs in nested loops.

Example:

for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
        System.out.println(i + j);
    }
}

Complexity:

O(n²)

O(n³) - Cubic Time

Occurs with three nested loops.

Example:

for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
        for(int k = 0; k < n; k++) {
            System.out.println(i + j + k);
        }
    }
}

Complexity:

O(n³)

O(2ⁿ) - Exponential Time

Typically occurs in brute-force recursion.

Example:

Naive Fibonacci Implementation.

public static int fib(int n) {

    if(n <= 1)
        return n;

    return fib(n - 1) + fib(n - 2);
}

Complexity:

O(2ⁿ)

O(n!) - Factorial Time

Occurs when generating all permutations.

Example:

For:

ABC

Permutations:

ABC
ACB
BAC
BCA
CAB
CBA

Total:

3! = 6

Complexity:

O(n!)

Complexity Growth Order

From fastest to slowest:

O(1)
↓
O(log n)
↓
O(n)
↓
O(n log n)
↓
O(n²)
↓
O(n³)
↓
O(2ⁿ)
↓
O(n!)

Rules for Calculating Big O

Rule 1: Ignore Constants

Example:

for(int i = 0; i < 5 * n; i++) {
}

Complexity:

O(5n)

Simplified:

O(n)

Rule 2: Ignore Lower Order Terms

Expression:

n² + n + 10

Dominant term:

n²

Complexity:

O(n²)

Rule 3: Add Sequential Loops

for(int i = 0; i < n; i++) {
}

for(int j = 0; j < n; j++) {
}

Operations:

n + n = 2n

Complexity:

O(n)

Rule 4: Multiply Nested Loops

for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
    }
}

Operations:

n × n

Complexity:

O(n²)

Interview Examples

Example 1

for(int i = 0; i < n; i++) {
    System.out.println(i);
}

Answer:

O(n)

Example 2

for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
        System.out.println(i + j);
    }
}

Answer:

O(n²)

Example 3

for(int i = 1; i < n; i *= 2) {
    System.out.println(i);
}

Answer:

O(log n)

Best Case, Average Case, and Worst Case

CaseMeaning
Best CaseMinimum operations required
Average CaseExpected operations
Worst CaseMaximum operations required
CaseComplexity
Best CaseO(1)
Average CaseO(n)
Worst CaseO(n)

Summary Table

ComplexityNameExample
O(1)ConstantArray Access
O(log n)LogarithmicBinary Search
O(n)LinearArray Traversal
O(n log n)LinearithmicMerge Sort
O(n²)QuadraticBubble Sort
O(n³)CubicTriple Nested Loops
O(2ⁿ)ExponentialRecursive Fibonacci
O(n!)FactorialPermutations

Key Takeaways

  • Asymptotic notation helps compare algorithms independent of hardware.
  • Big O represents the worst-case complexity.
  • Omega represents the best-case complexity.
  • Theta represents the exact growth rate.
  • Ignore constants and lower-order terms when calculating complexity.
  • Focus on the dominant term.
  • Efficient algorithms generally have complexity O(log n), O(n), or O(n log n).
  • Avoid O(n²), O(2ⁿ), and O(n!) solutions when possible for large inputs.

Mastering asymptotic notation is the first major step toward understanding Data Structures and Algorithms because every algorithm is evaluated based on its time and space complexity.