Big O Notation - potatoscript/dsa GitHub Wiki
π§ What is Big O?
Big O Notation is a way to measure how fast or how much memory a program uses as the input grows.
Think of it like this:
π¦ "If I add more boxes to my room, how much longer will it take me to clean it?"
π¦ Real-Life Example: Candy Sorting π¬
Imagine you have a box of candies to sort:
Number of Candies | Time Needed |
---|---|
5 | π Short |
500 | πππππ Long |
500,000 | π΅ Way too long! |
Big O helps us predict how bad things will get as the number of candies (input size n
) increases.
π Why Do Programmers Use Big O?
To answer questions like:
- Will this app slow down if there are more users? π§π§π§π§
- Will this function still be fast with 1,000,000 items? π§
- Which algorithm is faster or more efficient? ποΈ vs π²
π§° Big O Common Types (With Cute Emoji Guides!)
Big O | Name | Speed | Emoji Metaphor |
---|---|---|---|
O(1) | Constant Time | β‘ Super Fast | Always same speed |
O(log n) | Logarithmic | π Smart Fast | Skips chunks |
O(n) | Linear | πΆ Normal | Step-by-step |
O(n log n) | Log-Linear | π΄ Decent | Mix of linear + smart |
O(nΒ²) | Quadratic | π Slow | Loops inside loops |
O(2βΏ) | Exponential | π’π’ Very Slow | Grows like crazy |
O(n!) | Factorial | π¦₯ Max Slow | Tries everything |
πͺ O(1): Constant Time
"No matter how many cookies πͺ you have, Iβll grab one in 1 second!"
int firstItem = items[0];
β Accessing a specific index in an array.
π¦ 5 items? 1 second.
π¦ 5 million items? Still 1 second.
π O(log n): Logarithmic Time
"I'm reading a book π, but instead of every page, I skip half each time."
Binary Search is like this:
int binarySearch(int[] arr, int target)
{
// Keep cutting the list in half
}
With 1,000 items, logβ(1000) β 10 steps!
β‘ VERY efficient for big data!
πΆ O(n): Linear Time
"I check each studentβs name in the list."
for (int i = 0; i < n; i++) {
Console.WriteLine(names[i]);
}
β±οΈ Time grows step-by-step with size.
π΄ O(n log n): Log-Linear Time
"Like a smart robot scanning rows π."
This is the speed of efficient sorting algorithms:
- Merge Sort
- Quick Sort (average case)
π§ Mixes speed (log n) with checking every item (n)
π O(nΒ²): Quadratic Time
"Every student checks every other student."
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.WriteLine(i + "," + j);
}
}
π Nested loops = performance sinks for big data.
Items | Time |
---|---|
10 | 100 |
100 | 10,000 |
1000 | 1,000,000 π° |
π’ O(2βΏ): Exponential Time
Like doubling candy:
1 π¬ β 2 π¬ β 4 π¬ β 8 π¬ β ... π±
Used in brute-force or recursive tree problems.
Example: calculating Fibonacci recursively:
int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
Avoid this for big inputs!
π¦₯ O(n!): Factorial Time
"Try all combinations." Like arranging 5 books:
ABCDE
ACBDE
... (120 combinations)
Used in:
- Puzzle solvers π§©
- Permutations & brute-force
Too slow for anything more than 10 items. π₯
πͺ Visual Comparison Chart
Hereβs how the growth looks visually:
| Input Size (n) β
|
| π¦₯ O(n!)
| π’ O(2βΏ)
| π O(nΒ²)
| π΄ O(n log n)
| πΆ O(n)
| π O(log n)
|β‘ O(1)
+---------------------------------
π§ͺ Real Code Examples (C#)
O(1)
int GetFirstItem(int[] arr)
{
return arr[0]; // Always same time
}
O(n)
void PrintAllItems(int[] arr)
{
foreach (var item in arr)
Console.WriteLine(item); // n steps
}
O(nΒ²)
void CompareAllPairs(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
for (int j = 0; j < arr.Length; j++)
Console.WriteLine(arr[i] + ", " + arr[j]);
}
π Memory Complexity (Space)
Big O isn't just about speed! Also:
- πΎ How much space an algorithm uses
- π§Ί Temporary arrays, variables, etc.
Example:
int[] MakeCopy(int[] arr)
{
int[] newArr = new int[arr.Length]; // O(n) space
// ...
return newArr;
}
π§ Common Patterns Table
Code Pattern | Time Complexity |
---|---|
Single loop | O(n) |
Nested loop | O(nΒ²) |
Divide in half (binary search) | O(log n) |
Loop then divide | O(n log n) |
Constant operations | O(1) |
π¦ Summary β Youβve Learned:
- Big O shows how time/space grows with input.
- Common types: O(1), O(n), O(nΒ²), etc.
- Efficient code is better for large inputs!
- Choose the right algorithm, not just the correct one.