best, worst and average cases
counting the cost
what it is
For one size of input, a method can cost different amounts. A case says which inputs you count, the cheapest, the dearest or the mean over many.
Take 'look at every name', from counting basic steps, on a list of 4 names. Its four instructions, A to D, stand in the table below. With the name wanted at place 1, it runs 3 instructions: A, B and C. Each place further on costs 3 more. With the name missing, it runs 14. One size, five kinds of list, five costs.
| instruction | what it does |
|---|---|
| A | start at place 1 |
| B | if there is no name here, say the name is not there, and stop |
| C | if the name here is the one wanted, say where, and stop |
| D | move on to the next place, and go back to B |
| the name wanted | A | B | C | D | in all |
|---|---|---|---|---|---|
| at place 1 | 1 | 1 | 1 | 0 | 3 |
| at place 2 | 1 | 2 | 2 | 1 | 6 |
| at place 3 | 1 | 3 | 3 | 2 | 9 |
| at place 4 | 1 | 4 | 4 | 3 | 12 |
| not in the list | 1 | 5 | 4 | 4 | 14 |
The best case is the input of that size that costs least: 3. The worst case costs most: 14. The average case is no single input. It is the mean cost over a set of inputs, so it names the set. Over the 4 places, 3, 6, 9 and 12 make 30, and 30 over 4 is 7.5. Count the list without the name as a fifth input, and 30 + 14 = 44, and 44 over 5 is 8.8.
A bound, , or , says how a case's cost grows with the size. For names, the best case stays 3, so it is in . The worst, , 14 at 4 names, is in . The average over the places is , 7.5 at 4 names, a little over half of 14. It is in too. Questions 1 to 4 prove each.
- the worst case,
- the average,
- the best case, 3
The worst case is a guarantee: no input of that size costs more, whoever supplies it. The best case is seldom much use, since you rarely choose your input.
Sorts put numbers in order, each its own way, as the table shows, and differ in three more ways. First, a sort works in place when it needs no second list to work in. Second, a sort keeps equal numbers in order, and is called stable, when equal numbers end in the order they started. Selection sort does not. Sort Ann 20, Bo 20 and Cy 19, in name order, by age, as drawn: selection sort gives Cy, Bo, Ann, and the name order is lost.
| sort | how it works | in place | keeps equal numbers in order |
|---|---|---|---|
| bubble | swaps neighbours out of order, each walk one shorter; stops after a walk with no swap | yes | yes |
| insertion | walks each number left past bigger ones | yes | yes |
| selection | swaps the smallest left into each place in turn, unless it is there already | yes | no |
| merge | merges sorted stretches in pairs into a second list | no | yes |
Third, a sort may compare just as often whatever the order of its row. Selection sort makes 10 on every order of 5 numbers, so its best, worst and average are all 10 (question 5). Bubble and insertion sort make from 4, on a row in order, to 10, back to front (5, 4, 3, 2, 1). Insertion sort's 4 later numbers make at least 1 comparison each and at most one per number before: 4 to 1 + 2 + 3 + 4 = 10. One row can be dear for one and cheap for the other. On 2, 3, 4, 5, 1, bubble sort makes 10 and insertion sort 7, as question 5 draws.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: find the cheapest list
the line:why it is allowed: Every list has at least one name, so every list runs A, then B and C at place 1: at least 3. With the name wanted at place 1, C stops the method there.
the move: count the cheapest list
the line:why it is allowed: By line 1, A, B and C run once each.
guess it, thenthe move: add them
the line:why it is allowed: Three 1s make 3, at any length.
guess it, thenthe move: read it as a ceiling
the line:why it is allowed: By line 3, is 3 at every length, so at most 3.
guess it, thenthe move: read it as a bound
the line:why it is allowed: Big O of 1 needs a ceiling of a fixed number above zero times 1; by line 4, 3 serves.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: find the list that costs most
the line:why it is allowed: The method visits each place at most once and stops by place , past the end. So A runs at most once, B at most times, C and D at most times. Without the name, C never stops it, so each runs its most, as the table of runs shows.
the move: count that list
the line:why it is allowed: By line 1.
guess it, thenthe move: use the count of counting basic steps
the line:where does this come from?
question 1 of the page counting basic stepsHow many instructions run when the name is not in a list of names? the move: follow the walk
the line:why it is allowed: A starts at place 1. At each place with a name, B lets the method through, C looks and does not stop it, and D moves it on. At place , B finds no name and stops it.
the move: count instruction
the line:why it is allowed: A runs at the start, and nothing goes back to it.
the move: count instruction
the line:where the method is B C D place 1 runs runs runs place 2 runs runs runs place 3 runs runs runs place 4 runs runs runs place 5 runs, and stops in all 5 4 4 A list of 4 names without the name wanted: B runs 5 times, C and D 4 times each. why it is allowed: B runs at every place visited, places 1 to by line 1.
the move: count instruction
the line:why it is allowed: C runs at each place with a name, places 1 to by line 1.
the move: count instruction
the line:why it is allowed: D runs after each run of C, since by line 1 C never stops the method.
the move: add them up
the line:why it is allowed: Let count every run of every instruction. Add lines 2 to 5.
the move: add like with like
the line:why it is allowed: The three lots of make , and the two 1s make 2. For 4 names, 14.
the move: a fact for the ceiling
the line:why it is allowed: Another way of counting gives another exact formula. So trap between two multiples of . First a fact: is at least 1, so 2 is at most .
the move: a ceiling
the line:why it is allowed: By line 8, swap the 2 in line 7 for , which is no smaller.
the move: add like with like
the line:why it is allowed: and make .
the move: think of at every length at once
the line:names instructions run 5 times the names 1 5 5 2 8 10 4 14 20 10 32 50 100 302 500 At every length, the count stays at or under 5 times the names. why it is allowed: Lines 1 to 10 hold for every list of at least one name. So read as the whole graph of counts against lengths, not one point.
the move: read it as a bound
the line:names twice the names and 20 more 3 times the names 1 22 3 10 40 30 20 60 60 30 80 90 100 220 300 A made-up count breaks the ceiling of 3 times the names below 20 names and meets it at 20. Each name more adds 2 to the count and 3 to the ceiling. why it is allowed: is the group of counts that, from some length on, never climb above a fixed number above zero times . Short lists may break it, as in the table beside this line. By line 10, 5 serves for . Read is in big O of .
the move: a floor
the line:- the count of instructions run,
- the ceiling of line 10,
- the floor of this line,
A log-log plot: each step across or up is ten times the one before. Multiplying by the same number moves up the same distance. So a line always the same multiple of another stays the same distance above it: 50 sits as far above 30 as 500 above 300. The count, the heavy line, stays between and . why it is allowed: Leave the 2 out of line 7. What is left is smaller.
the move: read it as a bound
the line:why it is allowed: is the same with a floor: from some length on, never below a fixed number above zero times . By line 13, 3 serves for . Read is in big omega of .
the move: a ceiling and a floor together
the line:why it is allowed: By lines 10 and 13, stays between and ; lines 12 and 14 read both as bounds with the same term, . That is the tight bound, read is in theta of : grows like . Counting the basic steps gives 3 and 20 instead (question 4 of the page counting basic steps); the term stays .
why it is allowed: The box above opens question 1 of counting basic steps; its is this . Its line 7 adds the same runs, and its lines 8 to 15 put in . For 4 names, 14.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: count the list with the name at place k
the line:why it is allowed: A runs once, B and C at places 1 to , and D at each place before , as the table of runs at the top shows.
the move: average the cost over the places
the line:why it is allowed: Add line 1 over the places, then share the total among them. Read as add up, for from 1 to . For 4 names, 30 over 4 is 7.5.
guess it, thenthe move: add like with like
the line:why it is allowed: The 1 and the 1 taken away cancel, and three lots of make .
guess it, thenthe move: take out the 3
the line:why it is allowed: Each part has a 3. For 4 names, 3, 6, 9 and 12 add to 3 times the sum of 1, 2, 3 and 4.
guess it, thenthe move: put the 3 over n
the line:why it is allowed: times 3 is .
guess it, thenthe move: a fact about adding up
the line:Two staircases of 1 to 4 squares fill 4 columns of 5, 20 squares. One staircase is half, 10. why it is allowed: Write 1 to in a row, and again backwards under it. Each of the columns adds to , so the two rows hold , twice the sum. Halve it.
guess it, thenthe move: use line 6
the line:why it is allowed: By line 6, the sum in line 5 is .
guess it, thenthe move: write it as one fraction
the line:why it is allowed: Two fractions multiplied make one, tops multiplied over bottoms multiplied.
guess it, thenthe move: cancel the n
the line:why it is allowed: The on top and below divide out, as is above zero. For 4 names, 7.5.
guess it, thenthe move: multiply out the bracket
the line:why it is allowed: 3 times is and 3.
guess it, thenthe move: write 3 as 2 and 1
the line:why it is allowed: 3 is 2 and 1, written so that the worst case's shows.
guess it, thenthe move: halve each part
the line:names best average worst half the worst, plus 0.5 4 3 7.5 14 7.5 1,000 3 1,501.5 3,002 1,501.5 By questions 1 and 2 and line 9; the last column is line 13. why it is allowed: Half a total is half of each part.
guess it, thenthe move: set it beside the worst case
the line:why it is allowed: By line 12, the average is half of question 2's worst case, plus one half. For a long list the half hardly counts.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: start from the average
the line:why it is allowed: Line 9 of question 3.
the move: a fact for the ceiling
the line:why it is allowed: Big O of needs a ceiling of a fixed number times . As 1 is at most , add to both, and is at most . At 4, 5 is under 8.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 2, swap the in line 1 for , which is no smaller.
guess it, thenthe move: multiply the top
the line:why it is allowed: 3 times is .
guess it, thenthe move: halve it
the line:why it is allowed: Half of is .
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5, is at most 3 times from 1 name on, and 3 is above zero.
guess it, thenthe move: a floor
the line:why it is allowed: Leave the 1 out of in line 1. What is left is smaller.
guess it, thenthe move: read it as a bound
the line:- the average,
- the ceiling of line 5,
- the floor of line 7,
The average between its ceiling and floor, 1 to 10 names. At 4 names, 7.5, 12 and 6. why it is allowed: By line 7, is at least one and a half times from 1 name on, a fixed number above zero.
guess it, thenthe move: a ceiling and a floor together
the line:why it is allowed: Lines 6 and 8 give a ceiling and a floor with the same term, , the part in brackets. So the average grows like , as the worst case does.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: count the comparisons at place k
the line:Selection sort on 2, 3, 4, 5, 1. Its passes make 4, 3, 2 and 1 comparisons, each with one swap, 10 comparisons and 4 swaps in all. place filled compared with comparisons 1 places 2 to 5 4 2 places 3 to 5 3 3 places 4 and 5 2 4 place 5 1 in all 10 Selection sort on any row of 5 numbers. why it is allowed: Selection sort fills places 1 to in turn; the last number left is the biggest. For place , it compares the smallest so far with each number after place .
the move: add them over the places
the line:why it is allowed: Line 1 for places 1 to , added, for a row of at least 2 numbers.
guess it, thenthe move: write the sum out
the line:why it is allowed: Place 1 takes , each place after it one fewer, down to 1. For 5 numbers, 4 + 3 + 2 + 1; for 2 numbers, just 1.
guess it, thenthe move: add them the other way round
the line:why it is allowed: The same numbers in the other order make the same total, 1 + 2 + 3 + 4 for 5.
guess it, thenthe move: use the adding up of question 3
the line:why it is allowed: Line 6 of question 3 adds up 1 to , for any from 1. Here the last number is , at least 1, so put it for .
guess it, thenthe move: add the 1
the line:why it is allowed: and 1 make . For 5 numbers, 10.
guess it, thenthe move: read it
the line:Insertion sort on 2, 3, 4, 5, 1. The 3, 4 and 5 each stop after one comparison; the 1 walks to the front. 1 + 1 + 1 + 4 = 7. a row of 5 bubble sort insertion sort selection sort in order 4 4 10 2, 3, 4, 5, 1 10 7 10 back to front 10 10 10 every order, on average about 9.26 about 7.72 10 Comparisons on rows of 5, averaged over all 120 orders. Any of 5 can go first, any of the 4 left second, and so on, 5 × 4 × 3 × 2 × 1. Only selection sort's never change. why it is allowed: Line 1 depends on the row's length, never its order, so every order of 5 costs 10. Insertion sort's cost changes with the order, as drawn.
guess it, thenthe move: read it as cases
the line:why it is allowed: By lines 6 and 7, every row of numbers costs the same, 10 for 5. So its best, worst and average are one number.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
a worked example
The three cases on a list of 5 names.
- Best, the name wanted at place 1: A, B and C, 3 instructions.
- With the name at places 1 to 5: 3, 6, 9, 12 and 15, 3 for each place reached.
- Average over the 5 places: they make 45, and 45 over 5 is 9. gives 9 for 5.
- Worst, the name missing: 17 instructions. gives 17 for 5.
the trap
check yourself
For 'look at every name' on a list of 10 names, what do the best case, the worst case and the average over the 10 places cost?
the answer
3 at best and 32 at worst. Over the 10 places, 3, 6, 9 and on up to 30 make 165, and 165 over 10 is 16.5.
Selection sort makes 10 comparisons on every row of 5. Is every row then its best and worst case, for comparisons and for swaps?
the answer
For comparisons, yes. For swaps, no. A row in order needs none, and 2, 3, 4, 5, 1 needs 4, one at each place filled. A case is for one count.
Sort Ann 20, Bo 20 and Cy 19 by age with insertion sort instead. In what order do they end?
the answer
Cy, Ann, Bo. Insertion sort keeps equal numbers in order, so Ann stays before Bo.