impera Open Impera

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.

Worked example

Tracing

  1. Algorithm: let $n = 1$, $s = 0$. Repeat: $s = s + n$, $n = n + 2$, until $n > 7$. Output $s$.
  2. Trace: $(n, s)$ goes $(3, 1)$, $(5, 4)$, $(7, 9)$, $(9, 16)$.
  3. Output 16: the sum of the first four odd numbers.
Check yourself

Which property must every algorithm have?

  • It must be written as a flow chart

  • It must use a computer

  • It must sort numbers

  • It must stop after a finite number of steps

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.

Worked example

Sorting into ascending order

  1. List: 7, 3, 9, 2, 5.
  2. Pass 1: 3, 7, 2, 5, 9.
  3. Pass 2: 3, 2, 5, 7, 9.
  4. Pass 3: 2, 3, 5, 7, 9.
  5. 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?

  • In the middle

  • At the start of the list

  • At the end of the list

  • It could be anywhere

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.

Worked example

One quick sort pass

  1. List: 18, 4, 12, 9, 15, 6, 20. Middle item (4th of 7): pivot 9.
  2. Less than 9: 4, 6. Greater: 18, 12, 15, 20.
  3. New list: 4, 6, 9, 18, 12, 15, 20.
  4. 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?

  • To the end of the list each time

  • After the pivot, in reverse order

  • Before the pivot, in their original order

  • They are removed from the list

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.

Worked example

Bins of size 10

  1. Items: 4, 7, 2, 6, 3, 5. Total 27, so the lower bound is $\lceil 2.7 \rceil = 3$ bins.
  2. First fit: bin 1: 4, 2, 3; bin 2: 7; bin 3: 6; bin 4: 5. 4 bins.
  3. First fit decreasing (7, 6, 5, 4, 3, 2): bin 1: 7, 3; bin 2: 6, 4; bin 3: 5, 2. 3 bins.
  4. 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?

  • 4

  • 43

  • 5

  • 4.3

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.
Worked example

Scaling the time

  1. An order $n^2$ algorithm takes 0.5 s for 100 items.
  2. For 400 items: $n$ is 4 times larger, so time is $4^2 = 16$ times larger.
  3. 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?

  • 24 s

  • 9 s

  • 6 s

  • 12 s

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.

Worked example

Is it optimal?

  1. A first fit decreasing packing uses 5 bins. The lower bound is 4.
  2. 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?

  • One that is only used for sorting lists of numbers

  • One that finds a good solution but may not find the best one

  • One that never stops running on large problems

  • One that always finds the optimal solution quickly

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