Basis Lab

How Long It Takes

Best Case, Worst Case, and the One You Get

Last timeReading the Cost Off the Work

The same procedure has three costs depending on what it is handed. Which of the three to plan around is a question about the situation, not about the code.

Every cost quoted so far has been a single number for a single procedure.

That was a simplification. Hand the same code two different inputs of the

same size and it can do wildly different amounts of work.

FIG 1Best, worst and typical for four familiar procedures
bestworsttypicalgap between worst and ty
scanning a list for a va1331
insertion sort3551
simple quicksort4542
hash table lookup1313
The codes run constant, logarithmic, linear, linear times logarithmic, quadratic from one to five. The first two rows have a worst case on the same rung as the typical case, which makes them easy to reason about and easy to trust. The two marked rows are the dangerous ones: both are usually excellent and both have a worst case an entire rung worse, which is a gap that stays invisible in testing and then arrives in production on one particular input. The rest of the lesson is about that gap.

The one that is a promise

Of the three, only the worst case cannot be violated. Everything else is a

claim about luck or about data.

That matters in three situations. When there is a deadline, so that being slow

is a failure rather than an inconvenience. When the cost is paid by somebody

waiting. And when an outsider chooses the input, which turns the worst case

from an unlucky event into something anybody can request on demand.

FIG 2One procedure, four inputs of the same size
stepinput to simple quicksortcomparisons on 10000 itemswhich casewhy
1random orderabout 160 000typicaleach split lands near the middle
2already sortedabout 50 000 000worstevery split peels off one item
3sorted backwardsabout 50 000 000worstthe same, from the other end
4all values identicalabout 50 000 000worstno split separates anything
5random, with a random pivotabout 160 000typical, reliablythe input no longer chooses the pivot
5 steps
Three of the four worst cases are inputs anybody would call ordinary, which is the point: already sorted data is the most common shape real data arrives in, and a column of identical values is what a status field looks like. The gap is a factor of three hundred at ten thousand items and grows with the input. The last row is the standard repair, and it is worth understanding as a change of who chooses: with a randomly chosen pivot the cost no longer depends on the input order, so an adversary who knows the code still cannot produce the worst case on demand.

The typical case and its assumption

An average is taken over a distribution of inputs, and the distribution is an

assumption that usually goes unstated.

The lesson stops here

1 more paragraph to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Counting Steps, Not Seconds
  2. 02The Shape of the Curveopening only
  3. 03Why the Constants Get Droppedopening only
  4. 04Reading the Cost Off the Workopening only
  5. 05Best Case, Worst Case, and the One You Getyou are here
  6. 06Space Is a Cost As Wellopening only
  7. 07Occasionally Expensive, Usually Freeopening only
  8. 08When the Analysis Liesopening only

Read alongside