Big O Notation
This content is for DSA. Switch to the latest version for up-to-date documentation.
Big O notation is a way to describe how the running time or space requirements of an algorithm grow as the input size increases. It helps us compare the efficiency of different algorithms, especially for large inputs.
Why is it important?
Section titled “Why is it important?”- It helps us predict how fast or slow an algorithm will be as the problem gets bigger.
- It allows us to compare different solutions and pick the most efficient one.
- It gives us a common language to talk about performance.
Common Big O Notations
Section titled “Common Big O Notations”- O(1): Constant time (fastest)
- O(log n): Logarithmic time (e.g., binary search)
- O(n): Linear time (e.g., simple loops)
- O(n²): Quadratic time (e.g., nested loops)

Example
Section titled “Example”Suppose you have a list of numbers:
// O(n) example: print each numberfor (int i = 0; i < n; i++) { System.out.println(numbers[i]);}Here, the time it takes grows as the list gets bigger, so it’s O(n).
Algorithms & Notations
Section titled “Algorithms & Notations”| Algorithm | Best Case | Average Case | Worst Case |
|---|---|---|---|
| Linear Search | O(1) | O(n) | O(n) |
| Binary Search | O(1) | O(log n) | O(log n) |
| Bubble Sort | O(n) | O(n²) | O(n²) |
| Selection Sort | O(n²) | O(n²) | O(n²) |
| Insertion Sort | O(n) | O(n²) | O(n²) |