A Level Further Mathematics · Chapter 63 · Decision Mathematics · Decision 1
An algorithm is a precise list of instructions, and the best ones sort, search and pack quickly enough to stay useful as problems grow.
Sat-navs, online shops and delivery firms run algorithms millions of times a day to sort data, find records and fill vehicles. This chapter studies classic algorithms for these tasks, traces them by hand, and asks how their running time grows with the size of the problem.
In this chapter · jump to a section
Start here
Your route through this chapter
By the end of this chapter you will be able to:
- follow an algorithm written as steps or as a flow chart, including Euclid's algorithm
- sort a list by bubble sort and by quick sort, showing each pass
- search a sorted list by binary search
- find a lower bound for bin packing and pack by first fit, first-fit decreasing and full bins
- explain why some algorithms give good but not always best solutions
- use the order of an algorithm to estimate how its running time grows
Time: three sessions, about 4 hours, plus the practice ladder and the exam workshop.
You need first: confident arithmetic, careful reading of instructions, and powers and logarithms.
The big idea
The big idea
An algorithm is a finite sequence of precise instructions that solves a problem. It must work for every valid input, end after a finite number of steps, and leave nothing to judgement. The same algorithm, followed exactly, always gives the same result.
The diagram shows a short unsorted list. A person can sort it by eye, but a computer needs exact instructions: which items to compare, when to swap them, and when to stop. Those instructions are an algorithm.
Decision Mathematics
Decision Mathematics is the study of methods for making good decisions about arrangements, routes, schedules and allocations. Its tools are algorithms, networks and optimisation. This first chapter introduces the idea of an algorithm and the ways of judging one.
What makes a good algorithm
A good algorithm is correct, finite and efficient. Correct means it always solves the problem. Finite means it always stops. Efficient means it does not waste steps, so it stays fast as the input grows. Decision Mathematics studies all three.
Algorithms and computers
Computers follow instructions exactly and very quickly, but they cannot use judgement. That is why algorithms must be precise. A tiny ambiguity, such as which of two equal items to choose, must be settled by a rule, or different computers might give different answers.
Precision matters
Every instruction must be unambiguous. Phrases such as roughly in order or the biggest one nearby are not allowed. If two people follow an algorithm on the same input, they must reach exactly the same result at every step.
Heuristic algorithms
Some problems are too hard to solve perfectly in a reasonable time. A heuristic algorithm gives a good solution quickly, but not always the best. Bin packing algorithms in section 5 are heuristics: fast and usually close to optimal.
Tracing an algorithm
To trace an algorithm, follow its steps by hand and record the values of every variable in a table. Each row shows the state after one step. Tracing is how examination questions check that you understand exactly what an algorithm does.
Inputs, outputs and loops
Algorithms take inputs, process them, and produce outputs. Most contain loops, which repeat steps until a stopping condition is met. Knowing exactly when a loop stops is the key to tracing it correctly.
Algorithms everywhere
Cooking recipes, assembly instructions and long multiplication are algorithms of a kind. What distinguishes the algorithms in this course is that they are precise enough for a computer to follow, and that their efficiency can be measured.
Section 1
Algorithms and flow charts
Written steps and flow charts
Algorithms may be written as numbered steps or drawn as flow charts. In a flow chart, rounded boxes mark start and stop, rectangles hold instructions, and diamonds hold questions with yes and no branches. Both forms describe the same process.
Common mistake: stopping a loop one step early, before the condition that ends it is actually met. A loop stops only when its stopping condition is true. Stopping when the answer looks obvious, before the algorithm says so, gives a different output and loses the trace marks.
Euclid's algorithm
Euclid's algorithm finds the highest common factor of two whole numbers. Divide the larger by the smaller and find the remainder. Replace the larger by the smaller and the smaller by the remainder. Repeat until the remainder is zero; the last non-zero remainder is the answer.
Why Euclid's algorithm works
Any common factor of two numbers also divides their difference, and so divides the remainder on division. So each step keeps the same common factors while making the numbers smaller. When the remainder reaches zero, the last divisor is the highest common factor.
Trace tables
A trace table has a column for each variable and a row for each pass through the loop. Fill it in one step at a time, exactly as the algorithm says. Examiners award marks for each correct row, so neatness pays.
Decision boxes
In a flow chart, a decision box asks a yes or no question, such as is the remainder zero. The answer decides which arrow to follow. Read the question carefully, because reversing yes and no changes the whole path.
Counting steps
The number of times a loop runs tells you how long the algorithm takes. For Euclid's algorithm, it is the number of divisions. Counting such steps is the start of measuring efficiency, developed in section 6.
Variables in a trace
Each variable in an algorithm holds a value that may change as the algorithm runs. In Euclid's algorithm, the two numbers change at every step. A trace table records their values in order, so a reader can follow every change.
Algorithms with counters
Many algorithms keep a counter, such as the number of passes or swaps. Increase it exactly when the algorithm says, usually at the end of each loop. Counting at the wrong moment gives a total that is one too many or too few.
Checking the output
After tracing, check the output makes sense. For Euclid's algorithm, the answer should divide both starting numbers exactly. A quick division by 21 confirms that 1071 and 462 are both multiples, so the answer is right.
Writing your own algorithm
Some questions ask you to describe an algorithm in words. Number the steps, state the inputs, use a clear loop with a stopping condition, and state the output. Test your description on a small example to make sure it works.
Worked example 1: Euclid's algorithm
Use Euclid's algorithm to find the highest common factor of 1071 and 462.
| Working | Reason |
|---|---|
| Divide and keep the remainder | |
| ; | Repeat with the divisor and remainder |
| Highest common factor 21, after 3 divisions | The last non-zero remainder |
Flow charts and loops
In a flow chart, a loop appears as an arrow leading back to an earlier box. The decision box that controls it must eventually send the process onward, or the algorithm never stops. Checking that every loop can end is part of checking that an algorithm is finite.
Efficiency of Euclid's algorithm
Euclid's algorithm is remarkably fast. Even for numbers with hundreds of digits, it needs only a few hundred divisions, because the remainders shrink quickly. This speed is one reason it is still used in modern cryptography.
Worked example 2: a shorter trace
Use Euclid's algorithm on 252 and 105, recording each division.
| Working | Reason |
|---|---|
| First division and remainder | |
| ; | Continue until the remainder is zero |
| Highest common factor 21 | Again the last non-zero remainder |
Check yourself. How many divisions does Euclid's algorithm make on 84 and 36?
Show the answer
First , then . So the algorithm makes 2 divisions, and the highest common factor is 12.
Section 2
Bubble sort
How bubble sort works
Bubble sort works along the list comparing neighbouring items. If a pair is in the wrong order, it swaps them. After one complete pass, the largest item has moved to the end. Further passes deal with the rest, until a pass makes no swaps.
Common mistake: stopping bubble sort after the last pass that made a swap, without the pass that confirms the list is sorted. The algorithm can only tell that the list is sorted by making a whole pass with no swaps. That final pass is part of the algorithm and must be shown and counted.
Bubble sort, pass by pass
- Swaps in the latest pass
- 4
- Passes needed in total
- 4
Figure 63.1 steps through bubble sort one pass at a time. The highlighted bars at the right are already in their final places.
Recording passes
Write the list after each pass on a new line. Show any swaps made, or state the number. This is the standard layout in examinations and makes it easy to see the largest items settling at the end.
Shortening each pass
After the first pass, the last item is in place, so the second pass need not compare it. Each pass can be one comparison shorter than the one before. Some versions of the algorithm use this, but the stopping rule is the same.
Counting comparisons
On a list of items, the first pass makes comparisons. In the worst case, when the list is in reverse order, the total is comparisons. This grows with the square of , which matters in section 6.
Derivation: the greatest number of comparisons in bubble sort
With the shortened passes, the first pass makes comparisons, the next , and so on down to 1. The total is an arithmetic series.
Using the formula for the sum of the first integers gives
This is the worst case, for a list in reverse order.
Sorting into descending order
To sort into descending order, swap a pair when the first is smaller than the second. The smallest item then moves to the end on each pass. Read the question carefully to see which order is required.
Sorting words
Words are sorted alphabetically using the same rules, comparing letter by letter. Ties in the first letter are broken by the second, and so on. The algorithm is unchanged; only the comparison is different.
Best and worst cases
A list that is already sorted needs only one pass of bubble sort, with no swaps, so it is the best case. A list in reverse order needs the most passes and comparisons, so it is the worst case. Real lists usually lie between.
Stability
Bubble sort never swaps equal items, because it swaps only when one item is strictly larger. So equal items keep their original order. This property, called stability, matters when sorting records by one field while keeping an earlier ordering.
Counting swaps
The number of swaps measures how far the list is from sorted order. Each swap fixes exactly one pair that was out of order. The total number of swaps equals the number of out-of-order pairs in the original list.
Bubble sort on nearly sorted lists
If only a few items are out of place, bubble sort finishes in a few passes, because the confirming pass comes early. This makes it reasonable for small or nearly sorted lists, even though it is slow for large random ones.
Worked example 3: the first pass
Carry out the first pass of bubble sort to put 8, 3, 6, 2, 9, 4 into ascending order.
| Working | Reason |
|---|---|
| Compare 8 and 3: swap; 8 and 6: swap; 8 and 2: swap | Larger item moves right |
| 8 and 9: no swap; 9 and 4: swap | 9 now moves to the end |
| After pass 1: 3, 6, 2, 8, 4, 9 with 4 swaps | The largest item is in place |
Bubble sort by hand
When sorting by hand, write each pass as a new row and underline the items already in place at the end. This makes the pattern clear and reduces mistakes. In examinations, show every pass, including the final one with no swaps.
Worked example 4: completing the sort
Complete the bubble sort of the list in worked example 3.
| Working | Reason |
|---|---|
| Pass 2: 3, 2, 6, 4, 8, 9 | Continue comparing each neighbouring pair |
| Pass 3: 2, 3, 4, 6, 8, 9 | The list is now in order |
| Pass 4: no swaps, so the algorithm stops | The confirming pass is needed |
Check yourself. How many comparisons does the first pass of bubble sort make on a list of 6 items?
Show the answer
Each neighbouring pair is compared once, and 6 items form 5 neighbouring pairs. So the first pass makes 5 comparisons.
Section 3
Quick sort
Pivots
Quick sort chooses a pivot, the middle item of the list. It then writes the items less than the pivot, in their original order, followed by the pivot, followed by the items greater than it. The pivot is now in its final place.
Common mistake: choosing the first item of the list as the quick sort pivot instead of the middle item. The standard method uses the middle item, rounding up for a list with an even number of items. Using the first item gives different passes and loses the method marks.
Sublists
After the first pass, the items each side of the pivot form two sublists. Quick sort is applied to each sublist separately, choosing a new middle pivot in each. The process continues until every sublist has one item or none.
Finding the middle item
For a list of items, the middle item is in position , rounded up if it is not a whole number. So a list of 7 items has its pivot in position 4, and a list of 8 items in position 5.
Why quick sort is quick
Each pass places pivots in their final positions and splits the problem into smaller pieces. On average, the list halves at each level, so far fewer comparisons are needed than in bubble sort. On a badly ordered list, though, it can still be slow.
Recording quick sort
Write the list after each pass, marking the pivots clearly, for example by underlining or circling them. Keep items in their original relative order within each sublist. Examiners look for the pivots and the order of the other items.
Sorting into descending order
For descending order, write the items greater than the pivot first, then the pivot, then the smaller items. The rest of the method is unchanged. As always, read the question to see which order it wants.
Choosing a good pivot
Quick sort works fastest when each pivot splits its sublist into two halves of similar size. The middle item is a reasonable choice, but if the list is unluckily ordered, the pivot may be near one end, making the split uneven and the sort slower.
Pivots are fixed
Once an item has been used as a pivot, it is in its final position and never moves again. Marking pivots clearly, by underlining or boxing them, helps you see which items still need sorting in later passes.
Equal items
If items equal to the pivot appear, the standard method in this course places them with the larger items or treats them consistently as the question directs. Most examination lists avoid repeated values to keep the method clear.
Recursion
Quick sort applies itself to smaller and smaller sublists. An algorithm that calls itself in this way is called recursive. Recursion is a powerful idea in computing, and quick sort is one of the clearest examples of it.
Worked example 5: the first pass
Carry out the first pass of quick sort to put 21, 7, 34, 15, 9, 28, 12 into ascending order.
| Working | Reason |
|---|---|
| Pivot is the 4th item, 15 | Middle of seven items |
| Less than 15: 7, 9, 12; greater: 21, 34, 28 | Keep their original order |
| After pass 1: 7, 9, 12, 15, 21, 34, 28 | The pivot 15 is now fixed |
Quick sort compared with bubble sort
Quick sort usually needs far fewer comparisons than bubble sort on long lists. For a list of a thousand items, bubble sort might need hundreds of thousands of comparisons, while quick sort typically needs around ten thousand. The difference grows with the size of the list.
Worked example 6: completing the sort
Complete the quick sort from worked example 5, stating how many passes are needed.
| Working | Reason |
|---|---|
| Pass 2: pivots 9 and 34, giving 7, 9, 12, 15, 21, 28, 34 | Middle item of each sublist |
| Pass 3: pivot 28 in the sublist 21, 28 | Only single items then remain |
| Sorted after 3 passes | All items are in place |
Check yourself. What is the first pivot when quick sort is applied to 15, 4, 23, 8, 16, 42, 11, 30?
Show the answer
There are 8 items, so the middle position is , rounded up to 5. The fifth item is 16, so 16 is the first pivot.
Section 4
Binary search
Searching a sorted list
Binary search finds an item in a sorted list by repeatedly halving the part still to be searched. Compare the target with the middle item. If it matches, stop. If the target is smaller, search the lower half; if larger, search the upper half.
Common mistake: applying binary search to a list that has not been sorted. Binary search discards half the list at each step on the basis that everything on one side is smaller. If the list is not sorted, the discarded half might contain the target, and the search fails.
The middle item
With the search range from position to position , the middle position is , rounded up. After each comparison, move or past the middle item and repeat. If the range becomes empty, the target is not in the list.
How fast is binary search?
Each comparison halves the range. A list of items needs at most about comparisons, more precisely the smallest whole number of halvings that reduces the list to nothing. A million items need only about 20 comparisons.
Searching for names
Binary search works on alphabetical lists of names in exactly the same way. Compare names letter by letter to decide whether the target comes before or after the middle name. Telephone directories and dictionaries are searched like this by hand.
Showing the working
For each comparison, write the current range, the middle position, the item there and the decision. Examiners expect this full record, not just the final answer. It also shows clearly when the target is absent.
When the target is absent
If the target is not in the list, the range eventually becomes empty. The algorithm then reports that the item is not present. The number of comparisons made is still counted, and it is usually the maximum for that list size.
Binary search and decision trees
Each comparison in binary search has two possible outcomes, so the search can be drawn as a branching tree. A tree of depth has at most positions, which is why at most comparisons are needed.
Comparing with linear search
A linear search checks items one by one from the start, needing up to comparisons. For large lists, binary search is vastly faster, but it needs the list to be sorted first. If a list is searched many times, sorting it first is worthwhile.
Records and keys
In practice, binary search looks up records by a key, such as a surname or an account number. The records must be sorted by that key. The method is identical; only the comparison of keys changes.
Middle position rounding
When the range has an even number of items, there are two middle positions. The convention in this course is to round up, taking the later one. Using the convention consistently matters, because a different choice changes which items are compared.
Worked example 7: finding an item
Use binary search to find 27 in the list 3, 7, 11, 15, 19, 23, 27, 31, 35.
| Working | Reason |
|---|---|
| Positions 1 to 9: middle 5, item 19; 27 is larger | Search the upper half next |
| Positions 6 to 9: middle 8, item 31; 27 is smaller | Search the lower half next |
| Positions 6 to 7: middle 7, item 27; found | Three comparisons in all |
Searching for a missing item
If the target is not in the list, binary search narrows the range until it is empty. The last few comparisons show where the target would have been. Reporting that the item is absent, with the comparisons made, completes the answer.
Worked example 8: the most comparisons
What is the greatest number of comparisons binary search can need on a list of 9 items?
| Working | Reason |
|---|---|
| Each comparison roughly halves the range | The range shrinks as 9, 4, 2, 1 |
| After 4 comparisons the range is empty | Four halvings are enough |
| At most 4 comparisons | Equal to the smallest power of 2 above 9 |
Check yourself. In the list of worked example 7, how many comparisons are needed to find 35?
Show the answer
It compares 19, then 31, moving to the upper half each time, and then finds 35 in position 9. So it makes 3 comparisons.
Section 5
Bin packing
The problem
Bin packing places items of given sizes into bins of a fixed capacity, using as few bins as possible. Lorries, shelves, time slots and cutting stock all lead to bin packing problems. Finding the best packing is hard, so heuristic algorithms are used.
Common mistake: rounding the lower bound for bin packing down instead of up. The lower bound is the total size divided by the capacity. Any fraction of a bin needed means a whole extra bin, so the bound must be rounded up, never down.
Bin packing: first fit and first-fit decreasing
- Lower bound
- 4
- Bins used
- 5
Figure 63.2 packs seven items by first fit or first-fit decreasing. Change the capacity to see how the number of bins compares with the lower bound.
The lower bound
The total size of the items divided by the capacity, rounded up, is a lower bound: no packing can use fewer bins. If an algorithm reaches the lower bound, its packing is optimal. If not, a better packing may or may not exist.
Derivation: why the lower bound cannot be beaten
Suppose a packing uses bins of capacity . Every item lies in some bin, and no bin holds more than , so the total size satisfies
Since is a whole number, it is at least the smallest whole number not below .
So no packing can use fewer bins than this lower bound.
First fit
First fit takes the items in the order given and places each one in the first bin that has enough room. It is quick and simple, but the order of the items affects the result, and it often uses more bins than necessary.
First-fit decreasing
First-fit decreasing sorts the items into descending order first, then applies first fit. Packing large items first leaves small items to fill gaps, so it usually does better than first fit. It is still a heuristic, though, and is not guaranteed optimal.
Full bins
The full-bin method looks for combinations of items that fill bins exactly, by inspection. These are packed first, and the remaining items are packed by first fit. It often finds very good packings, but it relies on spotting combinations, so it is not a strict algorithm for large problems.
Why heuristics are used
The number of possible packings grows extremely fast with the number of items, so checking them all is impractical. Heuristics give good packings in a fraction of the time. Comparing their results with the lower bound shows how good they are.
Bin packing in practice
Loading lorries, scheduling adverts into breaks, and cutting lengths of pipe from stock are all bin packing problems. Real versions add extra constraints, such as weight limits or item shapes, but the basic heuristics in this section are still widely used.
Order matters for first fit
First fit depends on the order of the items. The same items in a different order may need more or fewer bins. First-fit decreasing removes this dependence by always using the same, sorted order.
Showing a packing
List the contents of each bin, for example bin 1: 8, 2. Examiners need to see the contents, not just the number of bins, so that they can check every placement. A table with one row per bin is the clearest layout.
The full-bin method in detail
To use full bins, look for groups of items whose sizes add exactly to the capacity, such as 9 and 3 for capacity 12. Pack those first, then use first fit on whatever is left. Write down each full combination you find.
Worked example 9: lower bound and first fit
Items of sizes 3, 8, 4, 7, 2, 6, 5, 2, 1 are packed into bins of capacity 10. Find the lower bound and the number of bins used by first fit.
| Working | Reason |
|---|---|
| Total 38, so lower bound | Total divided by capacity, rounded up |
| First fit: 3, 4, 2, 1 · 8, 2 · 7 · 6 · 5 | Each item to the first bin with room |
| First fit uses 5 bins | One more than the lower bound |
When heuristics fail
A heuristic may use more bins than necessary. For some lists, first-fit decreasing uses one more bin than the best packing. Comparing with the lower bound shows when an answer is definitely optimal, but a gap does not prove the heuristic was wrong.
Worked example 10: first-fit decreasing
Pack the same items by first-fit decreasing, and decide whether the packing is optimal.
| Working | Reason |
|---|---|
| Sorted: 8, 7, 6, 5, 4, 3, 2, 2, 1 | Sort into descending order first |
| Bins: 8, 2 · 7, 3 · 6, 4 · 5, 2, 1 | First fit on the sorted list |
| 4 bins, equal to the lower bound | So this packing is optimal |
Check yourself. Items with total size 40 go into bins of capacity 12. What is the lower bound?
Show the answer
Dividing gives , which must be rounded up because three bins hold only 36. So the lower bound is 4 bins.
Section 6
The order of an algorithm
Measuring efficiency
The order of an algorithm describes how its running time grows with the size of the input. If the time is roughly proportional to , the algorithm has quadratic order. Doubling the input then roughly quadruples the time.
Common mistake: scaling the run time in proportion to the size of the problem for an algorithm of quadratic or higher order. For an algorithm of order , multiplying the size by multiplies the time by . Scaling by alone badly underestimates how long a large problem will take.
How run time grows with the size of the problem
- Size scale factor
- 4
- Predicted time
- 32 s
Figure 63.3 compares how run time grows for algorithms of different orders. The steeper curves belong to higher orders.
Orders of the algorithms in this chapter
Bubble sort has quadratic order, since it can need comparisons. First fit is also quadratic in the worst case. Binary search has logarithmic order, which grows extremely slowly. Quick sort is usually much faster than quadratic.
Using the order to predict times
If an algorithm of order takes time for items, it takes about for items. This assumes the same computer and similar data. It gives estimates, not exact times.
Why order matters more than speed
A faster computer reduces every time by a fixed factor. A better algorithm changes the order, which matters far more for large inputs. An algorithm of order on a fast computer will eventually be beaten by one of order on a slow one.
Working backwards
The same formula finds the largest problem that can be solved in a given time. Rearranging gives . This answers questions such as how many items can be sorted in one minute.
Estimates, not guarantees
Order describes the general trend for large inputs. For small inputs, constant overheads can dominate. Real times also depend on the data, such as how nearly sorted a list already is. Treat predictions from the order as estimates.
Comparing algorithms
When two algorithms solve the same problem, compare their orders. For large inputs, the one with the lower order will eventually be faster, whatever the constants. This is why computer scientists care so much about order.
Limitations of the order model
The order ignores constant factors and lower-order terms. For small inputs, an algorithm of higher order with a small constant may be faster. Predictions are most reliable when the new input is much larger than the one measured.
Units of time
Keep the same units throughout a calculation, whether seconds or milliseconds. Converting at the end avoids errors. State the final time with its units and say that it is an estimate.
A worked comparison
Suppose one algorithm has order and another order . For 1000 items, the first does about a million steps and the second about ten thousand. Even if the second is slower per step, it wins comfortably on large inputs.
Worked example 11: quadratic scaling
A bubble sort takes 2 seconds to sort 500 items. Estimate the time to sort 2000 items.
| Working | Reason |
|---|---|
| Size multiplied by | Find the scale factor for n |
| Bubble sort has quadratic order | Time scales with the square |
| Time seconds | Not 8 seconds, which is linear |
Logarithmic order
Binary search has logarithmic order: doubling the size of the list adds only one more comparison. This makes it extremely efficient for large lists, and it is why sorted data structures are so widely used in computing.
Choosing an algorithm in practice
For a short list, any sorting method is quick enough, and simple ones such as bubble sort are easy to program. For long lists, an algorithm of lower order is essential. Matching the algorithm to the size of the problem is part of good decision making.
Worked example 12: a cubic algorithm
An algorithm of order takes 0.5 seconds for 100 items. Estimate its time for 300 items.
| Working | Reason |
|---|---|
| Scale factor | Ratio of the problem sizes |
| Time multiplied by | Cubic order raises to the third power |
| Time seconds | An estimate for similar data |
Check yourself. For a list of 6 items, what is the most comparisons bubble sort can make?
Show the answer
The greatest number is comparisons, reached when the list starts in reverse order.
Reference
Results in this chapter
| Result | What it says | When to use it | In the formula booklet? |
|---|---|---|---|
| Last non-zero remainder | Euclid's highest common factor | Tracing Euclid's algorithm | No, learn it |
| Stop after a pass with no swaps | Bubble sort stopping rule | Every bubble sort | No, learn it |
| Pivot in position | Quick sort pivot | Every quick sort | No, learn it |
| At most comparisons | Binary search | Searching sorted lists | No, learn it |
| Lower bound | Bin packing | Judging a packing | No, learn it |
| Time when size | Order | Estimating run times | No, learn it |
Reasoning
Modelling task: packing parcels into delivery vans
The situation
A courier has parcels of volumes 3, 8, 4, 7, 2, 6, 5, 2 and 1 cubic metres to deliver, and vans that each hold 10 cubic metres. The manager wants to use as few vans as possible, and to decide quickly each morning without trying every arrangement.
Assumptions
Each parcel is treated as a single volume that fits anywhere in a van, so only total volume matters. Parcels cannot be split between vans. Every van has the same capacity, and the shape, weight and delivery order of the parcels are ignored.
These assumptions turn the courier's problem into one-dimensional bin packing, which the algorithms of section 5 can handle.
The model
The parcels are items and the vans are bins of capacity 10. The total volume is 38, so the lower bound is vans. First fit and first-fit decreasing are applied, and their results compared with this bound.
Results
First fit, taking parcels in the given order, uses 5 vans. First-fit decreasing, sorting the parcels from largest to smallest first, uses 4 vans: 8 and 2, 7 and 3, 6 and 4, then 5, 2 and 1. Since 4 equals the lower bound, this packing is optimal for the model.
Evaluating the model
The model ignores the shapes of parcels, so a real van might not fit 10 cubic metres of awkwardly shaped boxes. It also ignores weight limits and delivery routes, which might make some combinations impractical even if they fit.
A better model might add a weight constraint, leading to two-dimensional bin packing, or group parcels by delivery area first. The heuristic approach would still be valuable, because the real problem is even harder to solve exactly.
Your turn
Suppose the van capacity is increased to 12 cubic metres. Use first-fit decreasing to pack the same parcels, and decide whether the result is optimal.
Show a model answer
The lower bound is . First-fit decreasing packs 8, 4 · 7, 5 · 6, 3, 2, 1 · 2, using 4 vans. That equals the lower bound, so it is optimal, though the larger vans have not reduced the number needed.
Practice
Practice ladder
Rung 1: Recall and fluency
Q1. Find the highest common factor of 119 and 85 by Euclid's algorithm, stating how many divisions it makes.
Worked solution to Q1
Divide repeatedly, keeping each remainder: , then , then . The remainder is now zero, so the highest common factor is the last non-zero remainder, 17, found after 3 divisions.
Q2. Carry out the first pass of bubble sort to put 5, 1, 4, 2, 8 into ascending order, counting the swaps.
Worked solution to Q2
Comparing neighbours, 5 swaps with 1, then with 4, then with 2, and 5 and 8 stay. The list becomes 1, 4, 2, 5, 8 after 3 swaps in this first pass.
Q3. State the greatest number of comparisons bubble sort can need on 10 items.
Worked solution to Q3
The passes need at most 9, 8, 7 and so on down to 1 comparisons. Their sum is , reached when the list is in reverse order.
Rung 2: Application
Q4. Complete the bubble sort started in Q2, stating the total number of passes.
Worked solution to Q4
The second pass swaps 4 and 2, giving 1, 2, 4, 5, 8. The third pass makes no swaps, which confirms the list is sorted, so 3 passes are needed in total.
Q5. Show the list after the first pass of quick sort on 15, 4, 23, 8, 16, 42, 11, 30.
Worked solution to Q5
The pivot is the fifth of eight items, 16. Writing the smaller items in order, then 16, then the larger ones gives 15, 4, 8, 11, 16, 23, 42, 30.
Q6. How many comparisons does binary search need to find 3 in the list 3, 7, 11, 15, 19, 23, 27, 31, 35?
Worked solution to Q6
The search compares 19, then 11, then 7, each time moving to the lower half, and finally finds 3. That is 4 comparisons, the most possible for nine items.
Rung 3: Analysis
Q7. At most, how many comparisons does binary search need on a list of 100 items?
Worked solution to Q7
Each comparison halves the range, so we need the smallest power of 2 above 100. Since , at most 7 comparisons are needed.
Q8. Find the lower bound for packing items 4, 8, 5, 7, 6, 3 into bins of capacity 10.
Worked solution to Q8
The total size is 33, and . Rounding up, because part of a fourth bin is needed, gives a lower bound of 4 bins.
Q9. Pack the items of Q8 by first fit, in the order given, and state the number of bins.
Worked solution to Q9
First fit places 4 and 5 together, 8 alone, 7 and 3 together, and 6 alone. That uses 4 bins, which equals the lower bound, so it is optimal here.
Rung 4: Stretch
Q10. Use first-fit decreasing on the items of Q8 and compare with Q9.
Worked solution to Q10
Sorted, the items are 8, 7, 6, 5, 4, 3. First-fit decreasing gives 8 · 7, 3 · 6, 4 · 5, using 4 bins, the same as first fit and the lower bound.
Q11. An algorithm of order takes 0.5 seconds for 100 items. Estimate its time for 300 items.
Worked solution to Q11
The size is multiplied by 3, so for a cubic algorithm the time is multiplied by . The estimated time is seconds.
Q12. A quadratic algorithm takes 2 seconds for 500 items. Compute the largest list it could sort in 50 seconds.
Worked solution to Q12
The time may grow by a factor of 25, so the size can grow by . So about items could be sorted in 50 seconds.
Exam practice
Exam technique and worked answer workshop
How these questions are marked
M1 marks go to applying the correct algorithm, choosing the right pivot or middle item, and finding the lower bound. A1 marks go to correct lists after each pass and correct bin contents. B1 marks go to stopping rules, statements of optimality and comments on efficiency.
Write the list after every pass. Mark pivots clearly in quick sort. For bin packing, list the contents of each bin, not just the number of bins. Always compare your packing with the lower bound.
Exam question 1 (6 marks · Paper 3 · about 8 minutes)
Use bubble sort to arrange 8, 3, 6, 2, 9, 4 into ascending order. Show the list after each pass and state the number of passes used. (6)
Mark scheme for exam question 1
Pass 1: 3, 6, 2, 8, 4, 9 M1 A1
Pass 2: 3, 2, 6, 4, 8, 9 A1
Pass 3: 2, 3, 4, 6, 8, 9 A1
Pass 4 with no swaps B1
4 passes A1
Exam question 2 (7 marks · Paper 3 · about 10 minutes)
Use quick sort to arrange 21, 7, 34, 15, 9, 28, 12 into ascending order. Show the result of each pass and identify the pivots used. (7)
Mark scheme for exam question 2
First pivot 15 B1
Pass 1: 7, 9, 12, 15, 21, 34, 28 M1 A1
Pivots 9 and 34 B1
Pass 2: 7, 9, 12, 15, 21, 28, 34 A1
Pass 3: pivot 28 in the sublist 21, 28 M1
Sorted after 3 passes A1
Exam question 3 (6 marks · Paper 3 · about 8 minutes)
The list 3, 7, 11, 15, 19, 23, 27, 31, 35 is sorted. Use binary search to locate 27, showing each comparison. State the greatest number of comparisons needed for any search of this list. (6)
Mark scheme for exam question 3
Middle item 19, reject the lower half M1 A1
Middle item 31, reject the upper half A1
Middle item 27, found A1
Greatest number of comparisons M1
4 A1
Exam question 4 (7 marks · Paper 3 · about 10 minutes)
Items of sizes 3, 8, 4, 7, 2, 6, 5, 2, 1 are to be packed into bins of capacity 10. Find a lower bound for the number of bins. Pack the items by first fit and by first-fit decreasing, and determine whether either packing is optimal. (7)
Mark scheme for exam question 4
Total 38 M1
Lower bound 4 A1
First fit: 3, 4, 2, 1 · 8, 2 · 7 · 6 · 5 M1 A1
First-fit decreasing: 8, 2 · 7, 3 · 6, 4 · 5, 2, 1 M1 A1
First-fit decreasing is optimal, matching the lower bound B1
Exam question 5 (5 marks · Paper 3 · about 7 minutes)
Record each division as Euclid's algorithm finds the highest common factor of 1071 and 462, and explain how you know when to stop. (5)
Mark scheme for exam question 5
M1
A1
A1
Stops when the remainder is zero B1
Highest common factor 21 A1
Exam question 6 (5 marks · Paper 3 · about 7 minutes)
A bubble sort takes 2 seconds to sort 500 items. Explain why bubble sort has quadratic order, and estimate the time needed to sort 2000 items. (5)
Mark scheme for exam question 6
Greatest number of comparisons M1
Proportional to for large B1
Size multiplied by 4 M1
Time multiplied by 16 M1
32 seconds A1
Exam question 7 (6 marks · Paper 3 · about 8 minutes)
Items of sizes 5, 9, 3, 7, 6, 2, 8 are to be packed into bins of capacity 12. Find the lower bound, use the full-bin method to pack the items, and decide whether the packing is optimal. (6)
Mark scheme for exam question 7
Total 40, lower bound 4 M1 A1
Full bins 9 + 3 and 7 + 5 M1 A1
Remaining 6, 2, 8 packed in two more bins A1
4 bins, equal to the lower bound, so optimal B1
Exam question 8 (6 marks · Paper 3 · about 8 minutes)
Determine the greatest number of comparisons that bubble sort could make on a list of 10 items. A computer makes these comparisons in 0.09 milliseconds; estimate the time for the worst case of a list of 100 items. (6)
Mark scheme for exam question 8
Sum M1
45 comparisons A1
Quadratic order B1
Size multiplied by 10 M1
Time multiplied by 100 M1
9 milliseconds A1
Annotated model answer: exam question 2
The list has seven items, so the first pivot is the fourth, 15. B1. Writing the smaller items in their original order, then 15, then the larger ones gives 7, 9, 12, 15, 21, 34, 28. M1 A1.
The two sublists have pivots 9 and 34, B1, giving 7, 9, 12, 15, 21, 28, 34. A1. The sublist 21, 28 then needs one more pass with pivot 28, M1, after which only single items remain, so the list is sorted after 3 passes. A1.
Annotated model answer: exam question 4
The total size is 38, M1, so the lower bound is bins. A1. First fit, in the given order, gives bins 3, 4, 2, 1 · 8, 2 · 7 · 6 · 5, using 5 bins. M1 A1.
Sorting into descending order and applying first fit gives 8, 2 · 7, 3 · 6, 4 · 5, 2, 1, M1 A1, which uses 4 bins. This equals the lower bound, so first-fit decreasing is optimal here, while first fit is not. B1.
Connections
Synoptic link
Sequences and series from the pure chapters supply the arithmetic series behind the bubble sort comparison count. Logarithms explain why binary search is so efficient.
Graphs, networks and planarity introduces the structures on which later algorithms work. Minimum spanning trees and shortest paths apply the same ideas of precise, efficient algorithms to networks.
Synoptic question 1. Explain why a heuristic algorithm is useful even though it does not always give the best answer.
Answer to synoptic question 1
A heuristic gives a good answer quickly, while finding the guaranteed best answer may take an impractically long time for large problems. Comparing its result with a bound shows how close to optimal it is.
Recap
Recap and next step
- An algorithm is a precise, finite list of instructions; trace it exactly, step by step.
- Euclid's algorithm finds a highest common factor as the last non-zero remainder.
- Bubble sort stops only after a pass with no swaps; it has quadratic order.
- Quick sort uses the middle item as pivot, splitting the list into sublists.
- Binary search halves a sorted list at each step, needing very few comparisons.
- Bin packing heuristics are judged against the lower bound, the total over the capacity rounded up.
Next chapter: Graphs, networks and planarity, which introduces the language of graphs used in the rest of Decision Mathematics.
For your exam board
This chapter covers Pearson Edexcel A Level Further Mathematics (9FM0) Decision Mathematics 1 section 1: algorithms and flow charts, bubble sort, quick sort, binary search, bin packing by first fit, first-fit decreasing and full bins with lower bounds, and the order of algorithms. It is assessed on Paper 3D, Decision Mathematics 1. Similar content appears in AQA (7367) and OCR (H245) discrete options.