Time complexity
How an algorithm's running time grows as the amount of data grows. It's written with Big O.
Example
Adding up a list is O(n): with 1,000 numbers, 1,000 additions; with 2,000, 2,000.
Test yourself: Time complexity
Measuring efficiency
An algorithm keeps a full copy of the data to go faster. What does it spend more of?
With 1 million 4-byte numbers, the copy takes about 4 MB more. You pay in memory to gain in time.
Measuring efficiency
Why is complexity studied before data structures?
To choose between an array and a linked list you need to know one reads in O(1) and the other in O(n): without the ruler, there's no way to compare.
Time complexity
A loop that runs 5n + 1 rounds has complexity O(___).
The 5 and the 1 don't change the shape of growth: with n = 1,000 it's 5,001 rounds, with n = 2,000 it's 10,001. Twice n, almost twice the work: O(n).
0/3
â–¶ PlayLessons that use it
Measuring efficiency
Fast and light on memory
Time complexity
Count steps, not seconds
Searching with a hash
From O(n²) to O(n) with a table
Rebuilding a tree
Root, left and right
Permutations
Every possible order
N-queens
None attacks another
0-1 knapsack
Take it or leave it
Fractional knapsack
Most value per kilo
Largest container
Move the shorter board
Maximum product cutting
Cut into threes
