AS Level · Further Decision Mathematics 1 WDM11

Algorithms

Revise Algorithms for Further Decision Mathematics 1 WDM11 (AS Level) — revision notes and instant AI marking. Free to start.

📖 Revision notes · preview

Summary — What This Chapter Is Really About

  • An algorithm is a set of precise instructions that, if followed exactly, solves a problem. It can be written as text, pseudocode, or a flow chart.
  • "Robot mode" is the golden rule of this whole chapter: never take a shortcut just because you can "see" the answer. Follow every instruction, in order, exactly as a machine would.
  • Order (complexity) of an algorithm tells you how the number of steps grows as the amount of input data grows — linear, quadratic, cubic, and so on.
  • Bubble sort repeatedly compares neighbouring pairs and swaps them if they're the wrong way round, "bubbling" the largest value to the end of the list each pass.
  • Quick sort picks a middle "pivot" value and splits the list into a sub-list of smaller values and a sub-list of bigger values, then repeats on each sub-list.
  • Bin packing algorithms (first-fit, first-fit decreasing, full-bin) try to pack objects into as few equal-sized bins as possible — never guaranteed to be perfect, just "good enough."
  • The lower bound for bin packing tells you the theoretical minimum number of bins you could ever need — your algorithm's answer should be compared against it.

1. General Algorithms

What is an algorithm?

Think about a recipe for baking a cake. It doesn't say "make a cake" and leave you to figure it out — it tells you exactly what to do, in exactly what order: crack two eggs, add 200g of flour, mix for 3 minutes, bake at 180°C for 25 minutes. That's an algorithm. It's a set of precise instructions that, if you follow them strictly, will always lead to the solution to a problem.

Algorithms matter hugely for computers because computers can't "use their initiative" — they can only follow instructions exactly. A robot vacuum cleaner follows an algorithm to decide where to go next. A satnav follows an algorithm to find your route. Even you, cooking dinner or building flat-pack furniture, are following an algorithm.

How can an algorithm be presented?

  • Written instructions — a numbered list of steps in plain words.
  • Pseudocode — a mixture of everyday words and very basic code-like commands (Let, If, While, Input, Output). You don't need to know a real programming language to read it.
  • Flow charts — a diagram using shapes to show the order of steps, especially useful when there are decisions or repeated loops.

Flow chart shapes

ShapeMeaningAlternative name
OvalStart or EndTermination
RectangleAn instruction
DiamondA question (yes/no branch)Decision box

Note: the word "print" is sometimes used instead of "output" for the final result.

Stay in "Robot Mode"
Exam questions usually use small lists, so it's tempting to just "see" the answer using common sense. Don't. You get marks for showing that you followed the algorithm's exact steps — not for getting the right final number. Write down every value the algorithm tells you to, and state the output separately at the end, even if it feels obvious.

Optimal vs. sufficient solutions

Not every algorithm gives you the perfect (optimal) answer — sometimes it just gives you an answer that's good enough for the situation. For example, a touring band might use a quick algorithm to plan their UK tour route. If it's out by 200 miles across a total journey of thousands of miles, that's not a big deal — but working out the mathematically perfect route might take far too long to be worth it. In real life, there's often a trade-off between an algorithm's speed/efficiency and the accuracy of what it produces.

Worked Example — Following a Flow Chart

An algorithm (shown as a flow chart) inputs a number n, then repeatedly tests values of k and p = k² to check whether n is a square number.

For n = 23:

nkpIs p > n?Is p = n?
2311NONO
24NONO
39NONO
416NONO
525YES

Output: 23 is not a square number.

Notice: once "Is p > n?" answers YES, the algorithm stops and outputs immediately — it never even checks "Is p = n?" on that final row. That's exactly the kind of precise, no-shortcuts following the examiners want to see.

Practice Question

An algorithm instructs: "Input a value x. If x is even, output 'even'. If x is odd, let y = x + 1 and output y." Trace the algorithm for x = 7.

2. Order (Complexity) of an Algorithm

The order of an algorithm describes how "complex" it is — specifically, how the maximum number of steps it needs grows as you feed it more input data. It's described using a mathematical function of n, the input size.

Common orders

OrderMeaningExample function
LinearSteps ∝ size of inputn
QuadraticSteps ∝ square of input
CubicSteps ∝ cube of input
OtherSquare roots, logs, exponentials, etc.√n, n log n, ...

For example, an algorithm that searches every item in a list, one at a time, to find a target value is of linear order — worst case, it checks all n items.

"Order" and "Complexity" are the same word
Examiners use these interchangeably. "Quadratic order" = "quadratic complexity." Don't panic if the question swaps the word on you.

Using order to estimate completion time

Since the order tells you how steps scale with input size, you can use it to estimate (not calculate exactly) how long a bigger input will take, using proportionality.

Rule of thumb Doubling input size on an order-nᵏ algorithm ⟹ time increases by roughly 2ᵏ
Linear (n): doubles → ×2 time. Quadratic (n²): doubles → ×4 time. Cubic (n³): doubles → ×8 time. For order √n: doubling input → time × √2.
Worked Example

An algorithm for sorting n values has order n ln n. It takes 0.12 seconds for a list of 3000 values. Estimate the time for 60,000 values.

0.12 seconds corresponds to order 3000 ln(3000).

Time ≈ 0.12 × [60000 ln(60000)] ÷ [3000 ln(3000)] = 3.298...

Estimated time ≈ 3.30 seconds (3 s.f.)

Practice Question

An algorithm of quadratic order takes 4 seconds to process an input of size 50. Estimate how long it takes to process an input of size 100.

3. Sorting Algorithms

Sorting algorithms arrange a list of items (usually numbers, but could be letters or words) into ascending or descending order. Easy for a human with a short list — but a computer needs a precise algorithm to do it reliably every time, even with thousands of items. Again: robot mode. Don't skip steps just because the answer looks obvious.

3.1 Bubble Sort

Imagine a bubble of air rising through water — each pass, the largest (or smallest, for descending) remaining value "bubbles" its way to the correct end of the list. Bubble sort does this by comparing neighbouring pairs: item 1 vs item 2, then item 2 vs item 3, and so on. If a pair is the wrong way round, swap them. If they're equal, that does not count as a swap.

One full sweep through the list like this is called a pass. After each pass, the item that ends up at the back of the working list is guaranteed to be in its final, correct position — so it can be dropped from the "working list" for future passes. This shrinking working list is what keeps bubble sort reasonably efficient.

Bubble Sort — When is it finished? Complete when a pass makes NO swaps, OR after the (n − 1)th pass
Both conditions mean the same thing in the end — but a pass with zero swaps is the clearest way to *prove* the list is sorted. Never skip stating the final "no swap" pass!

Counting passes, comparisons and swaps (for n items)

QuantityFormula / Logic
Maximum number of passesn − 1
Comparisons in the kth passn − k
Maximum total comparisons(n−1) + (n−2) + ... + 1 = ½n(n−1) → quadratic order
Maximum swaps in a passSame as comparisons in that pass (happens if list starts in reverse order)
Worked Example — Bubble Sort in Action

Sort 5, 7, 4, 9, 3, 5, 6 into ascending order. After pass 2, the list is: 4, 5, 3, 5, 6, 7, 9 (the last two items, 7 and 9, are already correctly placed).

Pass 3 — working list is now just: 4, 5, 3, 5, 6

Full list during pass 3Comparison
45356794 < 5, no swap
45356795 > 3, SWAP
43556795 = 5, no swap
43556795 < 6, no swap

1 swap made in pass 3. Two more passes are needed: pass 4 swaps 4 and 3 (one swap), and pass 5 has no swaps — so the algorithm is complete after pass 5, giving 3, 4, 5, 5, 6, 7, 9.

Practice Question

A list of 6 items is to be sorted using bubble sort. State (i) the maximum number of passes required, and (ii) the number of comparisons in the 2nd pass.

Practice Question

Perform one pass of bubble sort (ascending) on: 8, 3, 6, 1. Show every comparison and state the resulting list.

Common Mistake
Students often forget the final "no swaps" pass that proves the algorithm is complete — this is an easy mark to lose. Also double-check whether the question wants ascending or descending order; if you sorted the wrong way, don't restart — just say "reversing the list gives..." and give the final answer in the order actually asked for.

3.2 Quick Sort

Quick sort works completely differently to bubble sort. Instead of comparing neighbours, it picks a pivot — the middle value of the current sub-list — and splits everything else into two groups: values less than the pivot, and values greater than or equal to the pivot. Each of those groups becomes a new (smaller) sub-list, and the process repeats inside each one, picking a new pivot each time — until every single item has, at some point, been chosen as a pivot.

Quick Sort — Choosing the Pivot Pivot = the middle value of the sub-list (no reordering first!)
If there are two "middle" items (even-length sub-list), the convention is to always take the item in the upper position. Values equal to the pivot are conventionally grouped with the "greater than" side.

The three-step method (ascending order)

  1. Step 1: Find and highlight the pivot (middle value) of each current sub-list.
  2. Step 2: List items less than the pivot first (in their original order), then the pivot itself, then items greater than or equal to the pivot (in their original order). This creates two new sub-lists either side of the pivot.
  3. Step 3: Repeat steps 1–2 on every new sub-list until every item has been used as a pivot. For descending order, just flip which side "greater" and "less" values go on.
Worked Example — Quick Sort (Descending)

Sort 5, 3, 7, 2, 4, 5, 9, 6 into descending order.

Pass 1 — 8 items, pivot = 5th value = 4

Greater-than-pivot items first (descending), then pivot, then smaller items:

5, 7, 5, 9, 6, 4, 3, 2

Pass 2 — new sub-lists: {5,7,5,9,6} and {3,2}. Pivots: 5 (upper-middle of first) and 2.

7, 9, 6, 5, 5, 4, 3, 2

Pass 3 — sub-list {7,9,6}, pivot = 6 (no reordering needed); {3} already alone.

Pass 4 — sub-list {7,9}, pivot = 9 (upper position): → 9, 7

Pass 5 — {7} alone, becomes its own pivot.

Final sorted list: 9, 7, 6, 5, 5, 4, 3, 2

Exam Tip — Showing Pivots Clearly
Papers are scanned in black and white, so don't rely on colour to mark pivots! Use different styles: circle the pivot, underline it, or box it — anything visually distinct that survives greyscale scanning.
Practice Question

Perform pass 1 of quick sort (ascending order) on the list: 12, 4, 9, 15, 6, 20, 3. Identify the pivot and show the list after pass 1.

Bubble Sort vs Quick Sort — Don't Mix Them Up
Bubble sort compares adjacent pairs and swaps repeatedly across passes. Quick sort picks a pivot and splits the list into two groups around it. They look completely different on paper — if your working shows single swaps between neighbours, that's bubble sort; if you see a pivot value with two groups either side, that's quick sort.

4. Bin Packing Algorithms

Bin packing is about fitting a set of objects into as few equal-sized bins as possible. Think of loading identical delivery vans with parcels of different weights, or cutting short cable lengths from large reels — you want to use the fewest vans/reels possible. "Packing" here is a loose word: it could mean separating, cutting, stacking, or loading.

All three bin packing algorithms in this chapter are heuristic — meaning they give you a good, workable solution, but not necessarily the optimal (best possible) one, and not necessarily the only correct-looking answer.

4.1 The Lower Bound

Before packing anything, you can calculate the theoretical minimum number of bins you could possibly need — this is the lower bound. It's found by dividing the total size of all the objects by the size of one bin, then rounding up to the next whole number (since you can't have a fraction of a bin).

Lower Bound Formula LB ≥ (Total capacity of all objects) ÷ (Size of each bin)
Always round UP to the nearest integer — even 4.01 needs 5 bins, not 4. This formula is the same for every bin-packing algorithm; only the packing method that follows differs.

4.2 First-Fit Bin Packing

This is the simplest and fastest method: objects are packed in the order they're given — no sorting first. Each object goes into the first bin that has room for it. If it doesn't fit in bin 1, try bin 2, then bin 3, and so on, opening a brand-new bin only when nothing existing has space.

Its big advantage: speed. No time wasted sorting the list first. Its downside: it often uses more bins than truly necessary.

Worked Example — First-Fit

Weights (kg): 12, 16, 18, 8, 16, 12, 10, 4, 6, 10, 12, 16, 20, 10, 24. Each shelf holds 40 kg.

Lower bound: total = 194, 194 ÷ 40 = 4.85 → LB = 5

ShelfItems placed (in original order)
1121684
218166
3121010
4121610
520
624

First-fit uses 6 shelves — one more than the lower bound of 5. This shows first-fit doesn't always find the optimal solution.

4.3 First-Fit Decreasing Bin Packing

Exactly the same packing rule as first-fit — except you first sort the objects into decreasing (descending) order before packing them. Placing the biggest items first tends to leave more useful gaps for the smaller items later, which usually gets you much closer to (or exactly at) the optimal number of bins.

Where does the sorting come from?
A question might ask you to first use bubble sort or quick sort to arrange the values into descending order, then apply first-fit decreasing. If asked to just name a suitable sorting algorithm, "bubble sort" or "quick sort" are both valid answers.
Worked Example — First-Fit Decreasing

Same weights, now pre-sorted descending: 24, 20, 18, 16, 16, 16, 12, 12, 12, 10, 10, 10, 8, 6, 4. Shelf capacity 40 kg. Lower bound = 5.

ShelfItems placed
12416
22018
316168
41212124
51010106

This uses exactly 5 shelves — matching the lower bound, so this solution is optimal!

Practice Question

Objects of size 7, 5, 4, 9, 3, 6 are to be packed into bins of capacity 12 using first-fit (in the order given). List the contents of each bin.

4.4 Full-Bin Packing

This method takes a completely different approach: instead of working through objects in a fixed order, you inspect the whole list to spot combinations of objects that add up exactly to the bin's capacity, and pack those combinations first. Any objects left over (that can't be grouped into an exact-fit combination) are then packed using the ordinary first-fit method.

Full-bin packing often finds the optimal solution — but it can be slow and time-consuming for long lists, since you're hunting for combinations "by eye." It's not suitable when speed matters more than getting the perfect answer.

Worked Example — Full-Bin

Same weights: 12, 16, 18, 8, 16, 12, 10, 4, 6, 10, 12, 16, 20, 10, 24. Shelf capacity 40 kg.

Spot combinations that add to exactly 40:

ShelfItemsSum
112121640
2810101240
3162440
41620440

Remaining objects, in original order: 18, 6, 10. These all fit together in one more shelf (18+6+10=34 ≤ 40), packed via first-fit:

ShelfItems
518610

5 shelves used — matches the lower bound, so this is optimal!

Exam Tip — Full-Bin Bookkeeping
Since full-bin combinations are spotted "by inspection," it's easy to accidentally use an item twice or drop one entirely. Tick off each object as you place it, and at the end, count that your bins contain the exact same number of items as the original list.
Practice Question

Objects 5, 7, 3, 8, 2, 6, 9 are to be packed into bins of capacity 15 using full-bin packing. Find at least one full-bin combination, then pack the rest with first-fit.

What to Memorise

Algorithm
A set of precise instructions that, if followed exactly, solves a problem.
Robot Mode
Follow every step exactly, in order — never take a shortcut, even if the answer seems obvious.
Order / Complexity
A function of n describing how the max number of steps grows with input size (linear, quadratic, cubic...).
Bubble Sort — Complete when...
A full pass makes zero swaps, OR after the (n − 1)th pass.
Bubble Sort — Max Comparisons
½n(n − 1) — this is quadratic order.
Quick Sort — Pivot Rule
The middle value of a sub-list (upper of two middles if even-length). Complete when every item has been a pivot.
Lower Bound (Bin Packing)
LB ≥ Total capacity of objects ÷ bin size, rounded UP to the next integer.
First-Fit
Pack objects in given order, each into the first bin with enough room. Fast, not always optimal.
First-Fit Decreasing
Sort objects descending first, then apply first-fit. Usually much closer to optimal.
Full-Bin Packing
Spot combinations that exactly fill a bin by inspection; pack leftovers with first-fit. Often optimal, but slow.

Concepts Checklist

Exam Tips & Common Mistakes

Don't "See" the Answer
With small lists, it's tempting to skip straight to the sorted answer. Examiners want to see every comparison, every swap, every pivot — marks are awarded for the process, not just the final list.
Always State Completion
For bubble sort, explicitly say something like "there were no swaps in pass 5, so the algorithm is complete." For quick sort, note "every item has now been used as a pivot." This is often a specific mark on the scheme.
Ascending vs Descending
Double check the question! If you sort the wrong way, don't restart from scratch — just reverse your final list and clearly state you're doing so, then give the answer in the order actually asked for.
Bin Packing ≠ Guaranteed Optimal
First-fit and first-fit decreasing are heuristic — they can use more bins than the lower bound. Always calculate the lower bound and compare: if your answer matches it, you can say "this is optimal." If not, don't claim optimality — just state how many bins your algorithm used.
Don't Include Spare Capacity in Your Final Answer
It's fine to jot down remaining bin capacity as scratch working, but your final answer should list only which objects go in which bin — not the leftover space numbers.

Typical exam question patterns

  • Trace a flow chart / pseudocode algorithm through a table for a given input, then state the output.
  • State the maximum number of passes/comparisons/swaps for bubble sort on a list of n items.
  • Perform a specific pass of bubble sort given the list state after a previous pass.
  • Perform the full quick sort algorithm on a list, clearly marking pivots at each stage.
  • Calculate a lower bound, then apply first-fit / first-fit decreasing / full-bin packing and compare to the lower bound to discuss optimality.
  • Estimate an algorithm's completion time on a larger input, given its order and a known time for a smaller input.
Also in the full note
  • Exam Tips & Common Mistakes
What's inside
📖 Revision notes ✦ AI flashcards ✓ Instant AI marking

Read the full Algorithms notes free

That's the preview — create a free account to read the rest, plus flashcards and practice questions with instant AI marking. No credit card.

Unlock the full notes free →

More Further Decision Mathematics 1 topics