In the last lesson, we saw that the same problem can be solved slowly or quickly. But how do we compare two solutions fairly?
That is what Big-O notation is for. It sounds mathematical, but the idea is simple, and you will use it in every chapter from here on.
By the end of this lesson, you will understand:
- What Big-O notation means
- Why we don't just measure time in seconds
- The five most common Big-O types
- How to work out the Big-O of simple code
What Is Big-O?
Big-O describes how the amount of work grows as the input grows.
It doesn't say "this takes 2 seconds". It says something like: "If the input becomes 10 times bigger, the work becomes 10 times bigger."
We write it like O(n), where n means the size of the input: the number of items in a list, the number of students, the number of characters in a word.
Why Not Just Measure Seconds?
Measuring time in seconds sounds simpler, but it is unreliable:
- A fast laptop runs code quicker than an old phone.
- The same computer can be slower when other apps are running.
- Code that is fast with 10 items may be painfully slow with 1,000,000.
Big-O ignores the machine and focuses on the algorithm itself. That makes it a fair way to compare two solutions on any computer, in any language.
Big-O answers one question:what happens to my code when the input gets really big?
The Growth Analogy
Imagine you are a teacher with a register of n students. Different tasks take different amounts of effort as the class grows.
1. O(1): Constant Time
"Read the name of the first student." Whether the class has 10 students or 10,000, it takes one step.
static String getFirst(List<String> students) {
return students.get(0); // always one step
}2. O(n): Linear Time
"Call out every student's name for attendance." Twice as many students means twice the work.
static int findHighest(int[] marks) {
int highest = marks[0];
for (int mark : marks) { // visits every mark once
if (mark > highest) {
highest = mark;
}
}
return highest;
}3. O(n²): Quadratic Time
"Make every student shake hands with every other student." Doubling the class makes the work roughly four times bigger. This gets slow very quickly.
static boolean hasDuplicate(String[] names) {
for (int i = 0; i < names.length; i++) {
for (int j = i + 1; j < names.length; j++) { // compares every pair
if (names[i].equals(names[j])) {
return true;
}
}
}
return false;
}A loop inside a loop is the classic sign of O(n²).
4. O(log n): Logarithmic Time
"Find a name in an alphabetical register by opening it in the middle, then halving." Open the middle page. Is the name earlier or later? Throw away half and repeat. Each step cuts the problem in half.
static int binarySearch(String[] sortedNames, String target) {
int low = 0, high = sortedNames.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2; // same as (low + high) / 2, but can't overflow
int cmp = sortedNames[mid].compareTo(target);
if (cmp == 0) {
return mid; // found it
}
if (cmp < 0) {
low = mid + 1; // throw away the left half
} else {
high = mid - 1; // throw away the right half
}
}
return -1; // not in the list
}This only works because the list is sorted, the same idea as the sorted library shelves from the last lesson.
Why do Java and C write
low + (high - low) / 2? In those languages a number can only hold so much. With a huge array,low + highcan overflow and become negative. Writing it this way gives the same middle without ever adding the two big numbers. Python and JavaScript numbers don't have this limit, so the simpler form is fine there.
5. O(n log n): Linearithmic Time
This is the speed of good sorting algorithms, such as the ones behind Java's Arrays.sort(), Python's sorted() and JavaScript's .sort(). It is a little slower than O(n), but far faster than O(n²). We will see why in the sorting chapter.
Seeing the Difference
Here is roughly how many steps each type takes as the input grows:
| Input size (n) | O(1) | O(log n) | O(n) | O(n log n) | O(n²) |
|---|---|---|---|---|---|
| 10 | 1 | ~3 | 10 | ~33 | 100 |
| 100 | 1 | ~7 | 100 | ~664 | 10,000 |
| 1,000 | 1 | ~10 | 1,000 | ~9,966 | 1,000,000 |
Look at the last column. With 1,000 items, O(n²) needs a million steps, while O(log n) needs about ten. That is why choosing the right algorithm matters far more than buying a faster computer.
Two Simple Rules for Working Out Big-O
1. Drop the Constants
If a function loops through a list twice, it does about 2n steps. We still write O(n).
Big-O cares about the shape of the growth, not the exact count.
2. Keep Only the Biggest Term
If a function has a nested loop (n²) followed by a single loop (n), the total is n² + n. We write O(n²).
When n is large, the n² part is so much bigger that the n part barely matters.
Remember This
If you remember only one thing from this lesson, remember: Big-O measures how your code's work grows as the input grows. A single loop is usually O(n), and a loop inside a loop is usually O(n²).