Skip to content

alphaPlan · Data Structures, Algorithms & the Web · Cheat-sheet 04

Thinking about efficiency

Compare programs by how their work grows with the input, not by the stopwatch, and let that choose the structure and the sort.

Ideas to remember

  1. 01Big-O describes the shape of growth: as the input n grows, how does the amount of work grow with it?
  2. 02O(1) does not grow with n, O(n) doubles when the data doubles, O(n²) quadruples when the data doubles.
  3. 03Count the nested loops over the data: no loop is often O(1), one loop is usually O(n), a loop inside a loop is usually O(n²).
  4. 04Choosing the right data structure can change the shape of growth: a set turns an n² duplicate check or guest-list scan into n work.
  5. 05Trading a little memory for a lower shape of growth is a bargain you will make again and again.
  6. 06Selection sort is O(n²) and the built-in sorted is O(n log n): learn selection sort to understand sorting, use sorted to actually sort.

Words

n
The input size; you count the steps as a function of it instead of timing the program with a stopwatch.
O(1), O(n), O(n²)
Constant, linear, quadratic: the three shapes of growth a beginner meets most.
if one in seen:
A membership check on a set is roughly O(1), so the whole duplicate check becomes O(n) in one pass.
sorted(scores, reverse=True)
Returns a new sorted list, flipped when reverse=True, and leaves the original untouched.
scores.sort()
Does the same job as sorted but rearranges the original list in place.
sorted(words, key=len)
Sorts by whatever you choose, here the length of each word.

Do this

  • Write down each approach's Big-O, then imagine the input ten times larger, instead of racing them once on a tiny example.
  • Look back at any loop that runs an in test on a list and ask whether a set would change its Big-O.
  • Work on a copy with list(scores) when a function should leave the original list alone.
  • Use Big-O to compare shapes of growth, not to declare a winner without knowing how big the data actually gets.

Watch out

  • Big-O throws away constants: an O(n) approach with an expensive step can lose to an O(n²) approach with a cheap step until n gets large.
  • O(1) for a set lookup means on average, not guaranteed to be one step every time.
  • Small n is a real case: on twelve names, the simplest code you can read at a glance is usually the right answer.

Found something unclear, outdated or improvable? Suggest an improvement