⌂ Home← All A Level units

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 searchBinary search
RequirementWorks on any listThe list must be sorted
MethodCheck each item in turnCompare with the middle item and discard half each time
Worst casen 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
ENDWHILE

Try 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

ADTKey idea
Stack / queue / linked listLIFO, FIFO and pointer-linked nodes (see Unit 10)
Binary treeEach node has up to two children; in a binary search tree, left values are smaller and right values larger, giving fast search
GraphNodes connected by edges, possibly weighted or directed
DictionaryKey–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.

OrderNameExample
O(1)ConstantAccessing an array element by index
O(log n)LogarithmicBinary search
O(n)LinearLinear search
O(n²)Polynomial (quadratic)Bubble and insertion sort
O(2ⁿ)ExponentialSome 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

  1. Binary search needs sorted data: always say so.
  2. Trace recursion by showing each call and return value.
  3. Big O: quote the worst case and explain what n is.
  4. Insertion sort shifts items rather than swapping pairs.
  5. 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.

1. Linear search worst case for n items is…
2. Binary search requires…
3. Binary search halves the list…
4. Insertion sort…
5. Bubble sort can stop early when…
6. A binary tree node has at most…
7. A dictionary stores…
8. O(1) means…
9. O(n²) is typical of…
10. O(log n) is typical of…
11. Recursion means…
12. Without a base case, recursion causes…
13. Each recursive call is stored on…
14. Factorial(3) =
15. Big O describes…

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.

Was this page helpful?