Module 1: Big O Notation (The Speed Limit)
📚 Module 1: Big O Notation
Course ID: DSA-101
Subject: The Speed Limit
Imagine you have two friends who want to deliver a letter across the city.
- Friend A: Walks to the destination.
- Friend B: Drives a car.
If the distance is 10 miles, the car is faster. But what if the distance is 10,000 miles? Or what if there is a massive traffic jam? In programming, we need a way to talk about speed that doesn’t depend on how fast the “car” (The CPU) is. We use Big O.
🏗️ Step 1: The Three Most Common Limits
1. O(1) - Constant Time (The “Fastest”)
No matter how much data you have, the task takes the same amount of time.
- Analogy: Looking at the first page of a book. It takes the same time if the book has 10 pages or 10,000 pages.
2. O(n) - Linear Time (The “Normal”)
As the data grows, the time grows at the exact same rate.
- Analogy: Reading every page of a book. If you have twice as many pages, it takes twice as long.
3. O(n²) - Quadratic Time (The “Dangerous”)
If you double the data, the time increases by four times.
- Analogy: Comparing every page of a book with every other page. If you have 10 pages, you make 100 comparisons. If you have 20 pages, you make 400!
🏗️ Step 2: The Ranking (Fast to Slow)
- O(1) - Constant (Instant)
- O(log n) - Logarithmic (Very Fast - Binary Search)
- O(n) - Linear (Fair)
- O(n log n) - Linearithmic (Good - Efficient Sorting)
- O(n²) - Quadratic (Slow - Nested Loops)
- O(2ⁿ) - Exponential (Terrible - Brute Force)
🏗️ Step 3: Why do we ignore constants?
In Big O, we only care about the Long Term Trend.
- becomes just .
- Because when is 1 billion, that ”+ 5” and that “2x” don’t really matter compared to the massive scale of .
🥅 Module 1 Review
- Big O: A mathematical way to describe how much slower an algorithm gets as the input grows.
- Growth Rate: We care about the “Shape” of the curve, not the exact seconds.
- Worst Case: We usually talk about the worst-case scenario so we are never surprised by bad performance.
:::tip Slow Learner Note If you see a single for loop, it’s usually O(n). If you see a for loop inside another for loop, it’s usually O(n²). Simple! :::