Algorithms, decomposition, abstraction, searching & sorting — master the foundations of computer science.
Key definitions, concepts, and terminology for Topic 1
A recipe is a perfect everyday example of an algorithm. Consider making a cup of tea:
Every step is clear, unambiguous, and the process finishes β all the properties of a good algorithm. Notice it also includes sequence (steps in order), selection (optional milk), and iteration (you could say "stir 5 times").
Analogy: Imagine you need to organise a school trip. That's a huge task! Decomposition means breaking it into sub-problems:
Each sub-problem is now small enough to give to a different person. This is exactly how software developers work β they decompose a big program into modules that different team members build independently.
The London Underground map is a famous example of abstraction. The real Tube tunnels twist, turn, and vary in depth underground. But Harry Beck's map removes all that unnecessary detail and shows only what matters: which stations exist and how they connect.
In the exam, abstraction = removing unnecessary detail. If they ask "What is being abstracted away?", identify what's been left OUT (e.g., real distances, geography). If they ask "What is being kept?", identify what's essential (e.g., station names, connections).
More examples of abstraction:
playerScore abstracts away the memory address where the data is storedSequence is everywhere β brushing your teeth (wet brush β apply toothpaste β brush β rinse). The order matters!
Selection is every decision you make: "IF it's raining THEN take an umbrella ELSE wear sunglasses." In Python:
if temperature < 15:
print("Wear a coat")
else:
print("T-shirt weather!")
Iteration is any repetition. A count-controlled loop is like doing 20 press-ups (you know in advance how many). A condition-controlled loop is like stirring soup until it boils (you don't know when it will happen).
# Count-controlled (FOR)
for i in range(20):
print("Press-up", i + 1)
# Condition-controlled (WHILE)
while soup_temp < 100:
stir()
Flowcharts use arrows to show the flow of control. Decision diamonds always have two exits labelled Yes and No.
How it works: Start at the first item. Check each item one by one. If found, return its position. If the end is reached without finding it, the item is not in the list.
Works on both sorted and unsorted lists. Simple but slow for large datasets — worst case checks every item (O(n)).
Analogy: Linear search is like looking for a specific book on an unsorted bookshelf β you start at one end and check each book until you find it. If it's the last book, you've checked every single one.
Walkthrough: Searching for 42 in [10, 23, 42, 7, 15]:
Only 3 comparisons needed here (best case = 1, worst case = all items).
How it works: The list must be sorted. Find the middle item. If it matches, return its position. If the target is smaller, discard the right half. If larger, discard the left half. Repeat until found or the list is empty.
Much faster than linear search for large datasets — halves the search space each time (O(log n)). But the data must be sorted first.
Analogy: Binary search is like looking up a word in a dictionary. You don't start at page 1 β you open to the middle. If your word comes before that page alphabetically, you look in the first half. If after, the second half. Each time you halve the pages to search.
Walkthrough: Searching for 31 in [3, 7, 11, 15, 23, 31, 42]:
Only 2 comparisons! A linear search would have needed 6. For 1 million items, binary search needs at most 20 comparisons vs 1,000,000 for linear.
How it works: Compare adjacent pairs. If they are in the wrong order, swap them. Repeat passes through the list until no swaps are needed.
Simple to understand and implement. Inefficient for large lists (O(n²)). Good at detecting an already-sorted list (best case O(n)).
Analogy: Imagine bubbles rising in a fizzy drink β the largest values "bubble up" to the end of the list with each pass.
Walkthrough: Sorting [5, 3, 8, 1]:
Pass 1: [5,3,8,1] β swap β [3,5,8,1] β ok β [3,5,8,1] β swap β [3,5,1,8]
Pass 2: [3,5,1,8] β ok β [3,5,1,8] β swap β [3,1,5,8]
Pass 3: [3,1,5,8] β swap β [1,3,5,8]
β Sorted!
How it works: Divide the list in half repeatedly until each sub-list has one item. Then merge the sub-lists back together in order.
Much faster than bubble sort for large datasets (O(n log n)). Uses more memory because it creates new sub-lists. A divide-and-conquer algorithm.
Analogy: Imagine sorting a deck of cards by splitting the deck in half again and again until you have single cards, then merging them back in order β like a tournament bracket in reverse.
Walkthrough: Sorting [6, 3, 8, 2]:
Split: [6,3,8,2] β [6,3] and [8,2]
Split: [6,3] β [6] and [3] | [8,2] β [8] and [2]
Merge: [6]+[3] β [3,6] | [8]+[2] β [2,8]
Merge: [3,6]+[2,8] β [2,3,6,8] β
Bubble sort is simple but slow (O(nΒ²)), uses no extra memory. Merge sort is fast (O(n log n)) but uses extra memory for sub-lists. Examiners love asking you to compare them.
How it works: Take each item from the unsorted part and insert it into the correct position in the sorted part. Like sorting playing cards in your hand.
Efficient for small or nearly-sorted lists. Works in-place (doesn’t need extra memory). Worst case is O(n²).
Analogy: This is exactly how you sort playing cards in your hand. You pick up one card at a time and slide it into the right position among the cards you're already holding.
Walkthrough: Sorting [5, 2, 8, 1]:
Start: [5] | 2, 8, 1 (5 is the sorted part)
Take 2: insert before 5 β [2, 5] | 8, 1
Take 8: insert after 5 β [2, 5, 8] | 1
Take 1: insert before 2 β [1, 2, 5, 8] β
total ← 0).IF condition THEN ... ELSE ... ENDIFFOR i ← 1 TO n ... NEXT iWHILE condition DO ... ENDWHILEProblem: A teacher wants a program to calculate and display the average score from a class of 30 students. Let's apply computational thinking:
Break the problem into sub-problems:
What do we need? Student scores and the count (30). What do we ignore? Student names, subjects, dates β they're not needed for calculating an average.
total β 0
FOR i β 1 TO 30
INPUT score
total β total + score
NEXT i
average β total / 30
OUTPUT "The average score is: " + average
This uses sequence (steps run in order), iteration (FOR loop repeats 30 times), and variables (total, score, average). Selection isn't needed here, but we could add it: IF average >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail"
total = 0
for i in range(30):
score = int(input("Enter score: "))
total = total + score
average = total / 30
print("The average score is:", average)
Step through algorithms and see how they work
Searching for 7 in the list: [3, 8, 1, 7, 5, 2]
| Step | index | list[index] | Found? |
|---|
Searching for 23 in the sorted list: [3, 7, 11, 15, 23, 31, 42]
| Step | low | high | mid | list[mid] | Action |
|---|
Follow this flowchart and predict the output. What value does it print?
Apply your knowledge with hints available
Drag each item into the correct category.
Drag the steps into the correct order.
Complete this linear search algorithm. Click the hint button if you get stuck.
Test your memory by matching terms with definitions
Find all 8 matching pairs. Click two cards to flip them.
Build answers using sentence banks, then reveal the mark scheme
No hints, no help — prove your mastery
See how you performed across each sub-topic
Complete activities to see your results