A levels Further Mathematics · Decision mathematics
Algorithms
1What an algorithm is
An algorithm is a finite sequence of precise instructions for solving a problem. A good algorithm:
- has clear, unambiguous steps;
- works for every valid input;
- terminates after a finite number of steps.
Algorithms can be written in words, as a flow chart, or in pseudocode. When asked to trace an algorithm, record the values of every variable at each step in a table.
Tracing
- Algorithm: let $n = 1$, $s = 0$. Repeat: $s = s + n$, $n = n + 2$, until $n > 7$. Output $s$.
- Trace: $(n, s)$ goes $(3, 1)$, $(5, 4)$, $(7, 9)$, $(9, 16)$.
- Output 16: the sum of the first four odd numbers.
Check yourself
Which property must every algorithm have?
Show the answer
It must stop after a finite number of steps
An algorithm terminates.
2Bubble sort
Bubble sort compares neighbouring items and swaps them if they are in the wrong order. One pass takes the largest unsorted item to the end.
Repeat passes until a pass makes no swaps.
Sorting into ascending order
- List: 7, 3, 9, 2, 5.
- Pass 1: 3, 7, 2, 5, 9.
- Pass 2: 3, 2, 5, 7, 9.
- Pass 3: 2, 3, 5, 7, 9.
- Pass 4: no swaps, so the list is sorted.
For a list of $n$ items, at most $n - 1$ passes are needed, and pass $k$ needs only $n - k$ comparisons.
Check yourself
After the first pass of a bubble sort into ascending order, where is the largest number?
Show the answer
At the end of the list
Each pass "bubbles" the largest unsorted item to the end.
3Quick sort
Quick sort chooses a pivot (often the middle item) and splits the list into items less than the pivot (written before it) and items greater (written after it), keeping their order. The process is then repeated on each sublist.
One quick sort pass
- List: 18, 4, 12, 9, 15, 6, 20. Middle item (4th of 7): pivot 9.
- Less than 9: 4, 6. Greater: 18, 12, 15, 20.
- New list: 4, 6, 9, 18, 12, 15, 20.
- Next, choose pivots in each sublist (6 and 15) and continue until every item has been a pivot or is alone.
Quick sort is usually much faster than bubble sort on long lists.
Check yourself
In quick sort, where do items smaller than the pivot go?
Show the answer
Before the pivot, in their original order
The pivot ends up in its correct final place.
4Bin packing
Bin packing fits items of different sizes into the fewest bins of a fixed capacity.
- Lower bound: $\left\lceil \dfrac{\text{total of items}}{\text{bin capacity}} \right\rceil$ (round up).
- First fit: take items in the order given; put each in the first bin it fits.
- First fit decreasing: sort into decreasing order first, then use first fit.
First fit decreasing packs 7, 6, 5, 4, 3, 2 into 3 bins, matching the lower bound, so no packing can do better.
Bins of size 10
- Items: 4, 7, 2, 6, 3, 5. Total 27, so the lower bound is $\lceil 2.7 \rceil = 3$ bins.
- First fit: bin 1: 4, 2, 3; bin 2: 7; bin 3: 6; bin 4: 5. 4 bins.
- First fit decreasing (7, 6, 5, 4, 3, 2): bin 1: 7, 3; bin 2: 6, 4; bin 3: 5, 2. 3 bins.
- This equals the lower bound, so it is optimal.
Check yourself
Items total 43 and bins hold 10. What is the lower bound for the number of bins?
Show the answer
5
Always round up.
5Order of an algorithm
The order (or complexity) of an algorithm describes how its running time grows with the size $n$ of the problem.
- Order $n$ (linear): double $n$, double the time.
- Order $n^2$ (quadratic): double $n$, four times the time. Bubble sort is order $n^2$.
- Order $n^3$ (cubic): double $n$, eight times the time.
Scaling the time
- An order $n^2$ algorithm takes 0.5 s for 100 items.
- For 400 items: $n$ is 4 times larger, so time is $4^2 = 16$ times larger.
- Time $\approx 0.5 \times 16 = 8$ s.
Check yourself
An order n³ algorithm takes 3 s for n = 20. Roughly how long for n = 40?
Show the answer
24 s
2³ = 8 times longer.
6Heuristic algorithms
A heuristic algorithm gives a good solution quickly, but does not guarantee the best (optimal) one. First fit and first fit decreasing are heuristics.
You can check a heuristic answer against a lower bound: if they are equal, the answer is optimal.
Is it optimal?
- A first fit decreasing packing uses 5 bins. The lower bound is 4.
- We cannot tell whether 4 bins are possible: the heuristic might not have found the best packing, or the lower bound might not be achievable.
Heuristics are used because finding a guaranteed optimal solution can take far too long for large problems.
Check yourself
What is a heuristic algorithm?
Show the answer
One that finds a good solution but may not find the best one
Quick and practical, not guaranteed.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera