Big O
Big O notation describes how much an algorithm's work grows as the amount of data grows, called n. It doesn't measure seconds, but the shape of growth: if twice the data doubles the work, it's O(n). It looks at the worst case and ignores constants: 2n and n are both O(n). It's like comparing 2 recipes by how long it takes to cook for 10 or for 1,000 people, not by how long it takes today.
O(1), or constant time, means the work doesn't depend on how much data there is. Reading element 500 of a list by its position takes the same with 1,000 elements as with 1 million. Looking up a key in a hash table is also O(1) on average. It's like opening a locker knowing its number: it doesn't matter if there are 10 or 10,000 lockers.
O(n), or linear time, is when work grows in proportion to the data: twice the data, twice the work. It happens when going through every element 1 time, like summing a list or searching for a value one by one. With 1,000 elements it's 1,000 steps; with 1 million, 1 million. It's like reading a guest list to find a name: the more guests, the longer it takes.
O(n²), or quadratic time, shows up with 2 nested loops: for each element you go through all the others. With 10 elements it's 100 steps; with 1,000, 1 million; with 1 million, 1 trillion. That's why it quickly becomes impractical. It's like everyone at a party greeting everyone else: with 10 people it's quick, with 1,000 it lasts all night.
Sorted from best to worst: O(1) doesn't grow; O(log n) grows very slowly; O(n) grows just like the data; O(n log n), that of good sorting algorithms, a bit more; and O(n²) explodes. With 1 million items: 1, about 20, 1 million, about 20 million and 1 trillion steps. Picking the right complexity matters more than having a fast computer. It's like choosing between walking, biking, driving or flying: for short distances it doesn't matter, for long ones it does.
