Computational thinking and problem-solving
Cambridge AS & A Level Computer Science 9618 · Paper 4 · Unit 19
Paper 4 · Unit 19. Searching, sorting, ADTs, Big O and recursion.
1. Searching
| Linear search | Binary search | |
|---|---|---|
| Requirement | Works on any list | The list must be sorted |
| Method | Check each item in turn | Compare with the middle item and discard half each time |
| Worst case | n comparisons: O(n) | About log₂ n comparisons: O(log n) |
Low ← 1
High ← N
Found ← FALSE
WHILE Found = FALSE AND Low <= High
Mid ← (Low + High) DIV 2
IF List[Mid] = Target THEN
Found ← TRUE
ELSE
IF List[Mid] < Target THEN
Low ← Mid + 1
ELSE
High ← Mid - 1
ENDIF
ENDIF
ENDWHILETry it: binary search
Sorted list: 3, 8, 12, 17, 21, 26, 30, 35, 41, 47, 52, 60.
2. Sorting
Bubble sort repeatedly compares adjacent items and swaps them; a flag can stop early when no swaps occur. Insertion sort takes each item in turn and inserts it into the correct place in the already-sorted part. Both are O(n²) in the worst case; insertion sort is efficient for nearly sorted data.
FOR I ← 2 TO N
Key ← List[I]
J ← I - 1
WHILE J > 0 AND List[J] > Key
List[J + 1] ← List[J]
J ← J - 1
ENDWHILE
List[J + 1] ← Key
NEXT I
3. Abstract data types
| ADT | Key idea |
|---|---|
| Stack / queue / linked list | LIFO, FIFO and pointer-linked nodes (see Unit 10) |
| Binary tree | Each node has up to two children; in a binary search tree, left values are smaller and right values larger, giving fast search |
| Graph | Nodes connected by edges, possibly weighted or directed |
| Dictionary | Key–value pairs; look up a value by its key |
ADTs can be implemented from built-in types such as arrays and records, or from each other (e.g. a queue from a linked list).
4. Big O notation
Big O describes how an algorithm’s time or space grows as the input size n grows, using the worst case.
| Order | Name | Example |
|---|---|---|
| O(1) | Constant | Accessing an array element by index |
| O(log n) | Logarithmic | Binary search |
| O(n) | Linear | Linear search |
| O(n²) | Polynomial (quadratic) | Bubble and insertion sort |
| O(2ⁿ) | Exponential | Some recursive algorithms |
5. Recursion
A recursive subroutine calls itself. It needs a base case that stops the recursion and a general case that moves towards it. Each call is pushed onto the call stack with its parameters and return address; values are returned as the stack unwinds. Too many calls cause a stack overflow.
FUNCTION Factorial(N : INTEGER) RETURNS INTEGER
IF N = 0 THEN
RETURN 1 // base case
ELSE
RETURN N * Factorial(N - 1) // general case
ENDIF
ENDFUNCTION
6. Examiner tips
- Binary search needs sorted data: always say so.
- Trace recursion by showing each call and return value.
- Big O: quote the worst case and explain what n is.
- Insertion sort shifts items rather than swapping pairs.
- Binary search tree: insert by comparing at each node, going left if smaller.
7. Practice questions
Practice: answer and check instantly
Type a short answer, then press Check or Enter.
Show answer
It must be sorted.
Show answer
O(n).
Show answer
O(log n).
Show answer
O(n²).
Show answer
The base case.
Show answer
4 × 3 × 2 × 1.
Show answer
A dictionary.
Show answer
Left.
Exam-style questions
9. Compare linear search and binary search. [4]
Show mark guide
Linear works on unsorted data, binary needs sorted data [1] · linear checks each item in turn [1] · binary compares with the middle and halves the list each time [1] · binary is faster for large lists: O(log n) vs O(n) [1].
10. Explain how the call stack is used when a recursive function runs. [3]
Show mark guide
Each call pushes a stack frame with parameters, local variables and return address [1] · calls continue until the base case is reached [1] · frames are popped and values returned in reverse order as the stack unwinds [1].
8. Topic test: predict your grade
Topic test: predict your grade
15 questions, about 10 minutes. Answer them all, then press Mark my test.
Frequently asked questions
What is the difference between linear and binary search?
Linear search checks each item in turn and works on unsorted data. Binary search needs sorted data and halves the list each time, so it is much faster for large lists.
What does Big O notation mean?
Big O describes how the time or memory an algorithm needs grows as the input size grows, using the worst case. For example, binary search is O(log n).
What is recursion?
Recursion is when a subroutine calls itself. It needs a base case to stop and a general case that moves towards the base case.