What Big-O measures
In this course you will not write a new program for every topic. You will write one — your media library — and each lesson adds one command to it. By lesson 15 you will have a tool that sorts, indexes and searches data a dozen different ways, and you will know which way wins when.
Pick your theme now and stay with it: music, film, games or books. The text says "item"; you should be thinking of a song, a film or a book.
What Big-O actually measures
Big-O does not measure seconds. It measures how the work grows as the data grows — how many extra operations each new item costs you.
Three of them show up in this lesson alone:
| Notation | What it means | Example |
|---|---|---|
| O(1) | the work does not depend on n | fetching an element by index |
| O(log n) | n doubles, the work goes up by one step | binary search (lesson 6) |
| O(n) | the work grows in step with n | scanning the whole list |
In this lesson you implement linear search — O(n). It is not a bad algorithm; it is the baseline. Every structure you build later is measured against this number.
Best, average and worst case
Linear search over a library of a thousand items:
- best case — the item you want is first: 1 comparison;
- worst case — it is last, or it is not there at all: 1000 comparisons;
- average case — roughly n/2.
Big-O talks about the worst case unless it says otherwise. That is why linear search is O(n) even though it sometimes gets lucky on the first try.
What you will build here
Four things, in this order:
Item— the record type. Two of its fields are bounded, and that is not an accident.metrics.Counter— the measuring stick. The only code you are given.algo gen— the data generator. Without it you have nothing to measure.algo find— linear search with a counter. Your first measurement.