Paper 2 · J277/02 · unit 2.1

Algorithms

Decomposition, abstraction, flowcharts, trace tables, linear/binary search, bubble/insertion/merge sort. Practice and quiz — no required order.

Everything on this course is open. Skip, jump, retry. Nothing is locked and there is no required order.

Thinking, drawing, tracing, searching, sorting. This unit is paper thinking — you will write the same ideas in Python on the programming page. Jump to merge sort if you already know flowcharts.

2.1.1 Computational thinking

Three words. Exam questions often ask you to spot which one a sentence is describing.

Idea Meaning Tiny example
Decomposition Break a problem into smaller sub-problems A game: input, movement, collision, score, draw
Abstraction Ignore unnecessary detail; keep what matters Tube map: not every alley, just stations and lines
Algorithmic thinking Work out logical steps that solve it “If colliding with ghost and not powered, lose a life”

They stack: decompose the game, abstract the collision to rectangles, write an algorithm for each piece.

2.1.2 Designing, creating and refining algorithms

Inputs, processes, outputs

Every algorithm: what goes in, what you do, what comes out. Write them as a list before you draw anything.

Structure diagrams

A tree of boxes: the big problem on top, sub-problems below. That is decomposition on paper.

Flowcharts

Learn the shapes:

  • Oval — start / stop
  • Parallelogram — input / output
  • Rectangle — process
  • Diamond — decision (yes/no)
  • Arrows show flow; a loop is an arrow going back

Pseudocode / OCR Exam Reference Language (ERL)

Paper 2 is not “must be Python”. OCR has a reference language. Close enough to Python that your brain can translate:

age = input("Age?")
if age >= 18 then
  print("Adult")
else
  print("Child")
endif

Count-controlled: for i = 0 to 9. Condition-controlled: while x < 10. Arrays: names[0]. Nested if / for is expected.

You will practise ERL on the programming lesson too — same language, more reps.

Trace tables

A trace table is a dry-run on paper: one column per variable (and sometimes output), one row per step.

x = 3
y = 0
while x > 0
  y = y + x
  x = x - 1
endwhile
x y condition x>0
3 0 true
2 3 true
1 5 true
0 6 false — stop

Final y is 6. If you skip a row, the exam will catch you.

Errors and refining

  • Syntax — breaks the rules of the language (prnt, missing endif)
  • Logic — runs but wrong answer (off-by-one, > instead of >=)
  • Runtime — crashes (divide by zero, file not found)

Refining: complete a half-written algorithm, correct a line, add a condition. Nesting means an if or a loop inside another.

2.1.3 Searching and sorting

You must step through, not only name.

Linear search

Look at each item in order until you find the target or run out.

  • Works on unsorted data
  • Worst case: n checks (the item is last or missing)

Binary search

Only on sorted data. Look at the middle. If the target is smaller, throw away the right half; if larger, throw away the left. Repeat.

  • Far fewer checks on large lists
  • If the list is unsorted, binary search is wrong — say so

Example: find 7 in 1 3 4 7 9 11 15. Mid is 7 — found in one hit. Find 2: mid 7, go left 1 3 4, mid 3, go left 1, not found.

Bubble sort

Compare neighbours, swap if out of order, pass through the list. Biggest values bubble to the end. Repeat until a pass with no swaps.

Slow on large lists. Easy to trace.

Insertion sort

Build a sorted left portion. Take the next item and insert it into the right place (shifting others). Like sorting cards in your hand.

Merge sort

Divide and conquer. Split the list in half until pieces are size 1. Merge sorted pairs into larger sorted lists.

More memory (temporary lists). Efficient on large data compared with bubble.

You should be able to show one pass of bubble, one insert of insertion, and one merge of two small sorted lists.


Exercises include a full trace. Skip to the quiz or another unit whenever you like.

Practice · optional · answers on this page

Exercises

Do as many or as few as you like, in any order. Hints and a model answer sit under each task.

2.1.1 · e1 paper

Name the thinking

Abstraction, decomposition, or algorithmic thinking? 1. Splitting “make a platformer” into map, player, enemies, HUD. 2. Treating enemies as rectangles when checking hits, ignoring their sprites. 3. Writing the step-by-step for “if hit and not invincible, lose a life”.
Hint
Pieces vs ignoring detail vs the steps.
Show a model answer
1. Decomposition 2. Abstraction 3. Algorithmic thinking
2.1.2 · e2 paper

Trace table

Complete a trace for: total = 0 for i = 1 to 4 total = total + i next i Columns: i, total. What is total at the end?
Hint
i takes 1,2,3,4. Add each time.
Show a model answer
i=1 total=1 i=2 total=3 i=3 total=6 i=4 total=10 Final total = 10 (sum 1+2+3+4)
2.1.2 · e3 paper

Find the logic error

Password must be at least 8 characters. if len(password) > 8 then print("OK") else print("Too short") endif What is wrong? Write a corrected condition.
Hint
Off-by-one. “At least 8” includes 8.
Show a model answer
`>` rejects length 8, which should pass. Use `>= 8` or `> 7`.
2.1.3 · e4 paper

Binary search dry-run

Sorted list: 2, 5, 8, 11, 14, 17, 20 Search for 14. Write the middle item at each step and which half you keep, until found.
Hint
Seven items, middle index 3 (0-based) is 11, or 1-based mid 4 is 14 depending on how you pick mid — be consistent. Using items 1–7, mid = 4th = 11.
Show a model answer
Mid = 11. 14 > 11, keep right: 14, 17, 20 Mid = 17. 14 < 17, keep left: 14 Found 14. (If your mid on the full list is 14 immediately, say so — different mid conventions exist. The skill is discarding half each time.)
2.1.3 · e5 paper

One bubble pass

One left-to-right pass of bubble sort on: 4, 1, 3, 2 Write the list after each neighbour comparison/swap.
Hint
Compare 4 and 1, swap; then the new neighbours, and so on.
Show a model answer
4>1 swap → 1, 4, 3, 2 4>3 swap → 1, 3, 4, 2 4>2 swap → 1, 3, 2, 4 End of pass: 4 is in place.
2.1.3 · e6 think

Which search?

1. Phone book already in name order, 10,000 entries, find “Thomson”. 2. A bag of shuffled score chits, find whether 87 is in there. Which search, and why?
Hint
Binary needs sorted. Linear always works.
Show a model answer
1. Binary — sorted, large n 2. Linear — unsorted; sorting first might be more work than one scan

Check yourself · not a gateway

Quiz

Mark it, reveal it, or skip it. A low score does not close anything. Try again as often as you want.

1 2.1.1 Abstraction is…
2 2.1.2 In a flowchart, a diamond represents…
3 2.1.2 A program runs but prints the wrong total. This is best described as a…
4 2.1.3 Binary search requires the data to be…
5 2.1.3 In bubble sort, after the first full pass of a list, which statement is true?
6 2.1.3 Which algorithms are divide-and-conquer sorts in this spec? (Select all that apply)
7 2.1.3 Linear search can be used on an unsorted list.