1Introduction
A short look at Big-O
When you compare two data structures you cannot answer "which one is faster?" in seconds; the time depends on the computer, the language and whatever else is running. Instead you look at how many steps an operation takes and how that number grows as the data grows. The short way of writing that growth is called Big-O notation.
1Growth, not time
The time complexity of an operation expresses how many steps it takes when the data has size n. A step is a small piece of work that finishes in constant time, such as an assignment, a comparison or moving a pointer on by one node.
Big-O does not give the exact number of steps; it tells you how that number grows as n grows. For example, when you search for a value in a list of n elements you look at each element once. The number of steps is proportional to n; we write this as O(n).
2Four basic growth rates
- O(1), constant: the step count does not depend on
n. Accessing an array by index, or inserting at the head of a linked list. - O(log n), logarithmic: the remaining work halves at every step. Binary search in a sorted array.
- O(n), linear: each element is looked at once. Searching a list or printing every element.
- O(n²), quadratic: for each element, every element is looked at. Two nested loops.
You see how big the gap is when you look at the step counts:
| Complexity | n = 10 | n = 1,000 | n = 1,000,000 |
|---|---|---|---|
| O(1) | 1 | 1 | 1 |
| O(log n) | about 3 | about 10 | about 20 |
| O(n) | 10 | 1,000 | 1,000,000 |
| O(n²) | 100 | 1,000,000 | 1,000,000,000,000 |
For small n they are all fast. With a million elements, though, O(n²) means a trillion steps; even on a computer doing a billion steps per second that takes about 17 minutes. The same job in O(n) is done in a thousandth of a second.
3Constants and smaller terms are dropped
If a loop does three things for each element and there are five setup steps at the start, the total is 3n + 5 steps. In Big-O this is written O(n). The reason: as n grows, the factor 3 and the extra 5 do not change the shape of the growth; double the list and the time still roughly doubles.
For the same reason an operation taking n² + n steps is O(n²). When n is a million, n² is a trillion while n is only a million; the smaller term's effect disappears.
Dropping constants does not mean two O(n) operations finish in the same time. Big-O only compares how they behave as the data grows.
4The worst case
The same operation can take different amounts of time on two inputs of the same size. When searching a list, if the value is in the first node you find it in one step; if it is not in the list at all you look at all n nodes. Unless stated otherwise, complexity is given for the worst case. That way you know the operation never goes beyond that bound on any input.
5Time and space
Big-O is not only used for time. How much extra memory an operation needs beyond its input is called space complexity, and it is written the same way. Inserting at the head allocates space only for the new node, so its space complexity is O(1). An operation that makes a copy of the list has space complexity O(n).
6Big-O in linked list operations
Every linked list lesson has a complexity section. Here is a summary ahead of time:
| Operation | Time | Why |
|---|---|---|
| Insert at the head | O(1) | One allocation and two assignments. The length of the list does not matter. |
| Insert at the end (no tail) | O(n) | You have to traverse the whole list to find the last node. |
| Insert at the end (with tail) | O(1) | The last node is already known. |
| Search for a value | O(n) | In the worst case every node is looked at once. |
If we wrote the same table for an array, access by index would be O(1) and inserting at the front would be O(n). This is the idea from What is a data structure?, that no structure is best at everything, put into numbers.
7Try it
An operation takes 5n + 20 steps. How is it written in Big-O?
The answer is B. The constant factor 5 and the added 20 do not change the shape of the growth as n gets larger. The step count grows in direct proportion to n, so it is O(n).
Show the answer
The answer is B. The constant factor 5 and the added 20 do not change the shape of the growth as n gets larger. The step count grows in direct proportion to n, so it is O(n).
Why is inserting at the end of a singly linked list O(n) when no tail pointer is kept?
Lessons you mark show up in the curriculum. This is kept on this device only.