Algorithms: Complexity, Searching and Sorting - Worksheets, Questions and Revision

12 original exam-style questions - 10 pages of questions with a full mark scheme - free printable PDF.

Download PDFJump to mark scheme (page 11)Read the revision guide
« Previous: Problem Solving and ProgrammingNext: Programming Paradigms and OOP »
Revision Library
revisionlibrary.co.uk
A-Level · Component 02: Algorithms and Programming

A2.3 Algorithms: Complexity, Searching and Sorting

OCR H446 · Calculators not allowed · about 120 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
Efficient algorithms matter because the same task can be solved in many different ways, and some methods take far longer or use far more memory than others as the amount of data grows. This question tests your understanding of the basic ideas used to measure and describe algorithm efficiency.
(a)State what is meant by the time complexity of an algorithm.(2)
(b)Each description below describes how the number of operations an algorithm performs changes as the input size n increases. Write the correct Big O notation (A-D) next to each description.

Big O notation:
A) O(1)
B) O(n)
C) O(log n)
D) O(n2)

Descriptions:
(i) The number of operations stays the same no matter how large n becomes.
(ii) The number of operations roughly doubles each time n doubles.
(iii) The number of operations increases by only a fixed amount each time n doubles (e.g. halving the remaining search space each step).
(iv) The number of operations roughly quadruples each time n doubles.
(4)
(c)Explain why Big O notation usually describes the worst-case number of operations rather than the best-case.(2)
(d)An algorithm contains two nested loops, each running from 1 to n. Which Big O notation describes its time complexity?(1)
  • A) O(n)
  • B) O(log n)
  • C) O(n2)
  • D) O(2n)
(Total for Question 1 is 9 marks)
2
A stock-control system for a small hardware shop in Leeds stores unsold item codes in an array called stock. The array is not sorted. All arrays in this paper are indexed from 0, so an array with n items has indices 0 to n - 1. Pseudocode follows OCR's Exam Reference Language conventions (assignment <-, iteration FOR ... NEXT / WHILE ... ENDWHILE / DO ... UNTIL, selection IF ... ENDIF).
FUNCTION LinearSearch(stock, target) RETURNS INTEGER
  FOR i <- 0 TO LEN(stock) - 1
    IF stock[i] = target THEN
      RETURN i
    ENDIF
  NEXT i
  RETURN -1
ENDFUNCTION

stock = [12, 7, 19, 3, 25, 8]
(a)Complete the trace table below to show LinearSearch(stock, 25) being called. Add a row for each value of i tested, showing stock[i] and whether stock[i] = target is true or false.
istock[i]stock[i] = target?
0
1
2
3
4
(5)
(b)State the value returned by the function call in part (a).(1)
(c)The array is now searched for the value 30, which is not present. State the number of times the condition stock[i] = target is tested, and give the value returned by the function.(2)
(d)State, using Big O notation, the worst-case time complexity of linear search on a list of n items.(1)
(Total for Question 2 is 9 marks)
3
A school's exam-results system stores candidate numbers in ascending order in the array numbers. Binary search can be used because the array is sorted.

numbers = [3, 7, 12, 15, 19, 23, 28, 34, 41, 50] (indices 0 to 9)
FUNCTION BinarySearch(numbers, target) RETURNS INTEGER
  low <- 0
  high <- LEN(numbers) - 1
  WHILE low <= high
    mid <- (low + high) DIV 2
    IF numbers[mid] = target THEN
      RETURN mid
    ELSE IF numbers[mid] < target THEN
      low <- ______
    ELSE
      high <- ______
    ENDIF
  ENDWHILE
  RETURN -1
ENDFUNCTION
(a)Write the missing pseudocode for the two blank lines above, so that the algorithm correctly narrows the search range.(2)
(b)Complete the trace table below for the call BinarySearch(numbers, 34).
lowhighmidnumbers[mid]result of comparison
(4)
(c)State the index returned, and the number of times numbers[mid] is compared with the target.(2)
(d)Each element of numbers is stored using 2 bytes, and the array starts at hexadecimal memory address 0x4000. Calculate the hexadecimal memory address of the element found in part (c).(2)
(e)Give the 8-bit binary representation of the byte offset (14) used in the calculation in part (d).(1)
(Total for Question 3 is 11 marks)
4
A car park's number-plate recognition system checks whether a scanned number plate matches one already stored in a list.
(a)The list of stored number plates is not kept in any particular order. Explain why binary search could not be used to search this list directly, and state one thing that would need to be done first if binary search were to be used.(2)
(b)The car park has 1000 stored number plates. Calculate the maximum number of comparisons linear search would need to make to determine that a scanned plate is not in the list.(1)
(c)Once sorted, the maximum number of comparisons binary search needs for a list of n items can be found using ceil(log2(n + 1)). Calculate the maximum number of comparisons needed to search the sorted list of 1000 number plates.(2)
(d)State, giving a reason based on your answers to (b) and (c), which search algorithm is more time-efficient for this large list.(2)
(Total for Question 4 is 7 marks)
5
A DIY store in Cardiff records the prices (in pounds) of five items on a shelf label strip in the order [5, 2, 9, 1, 6]. They are sorted using the bubble sort algorithm below.
PROCEDURE BubbleSort(list)
  n <- LEN(list)
  DO
    swapped <- FALSE
    FOR i <- 0 TO n - 2
      IF list[i] > list[i + 1] THEN
        temp <- list[i]
        list[i] <- list[i + 1]
        list[i + 1] <- temp
        swapped <- TRUE
      ENDIF
    NEXT i
    n <- n - 1
  UNTIL swapped = FALSE
ENDPROCEDURE
(a)Explain the purpose of the swapped flag in the pseudocode above.(2)
(b)Complete the trace table below for BubbleSort([5, 2, 9, 1, 6]), showing the contents of the list, the value of swapped, and the value of n at the end of each full pass.
passlist at end of passswappedn
1
2
3
4
(6)
(c)State the total number of comparisons and the total number of swaps made during the whole sort in part (b).(2)
(d)Explain how the algorithm knows to stop after pass 4 rather than continuing to check further passes.(2)
(Total for Question 5 is 12 marks)
6
A leisure centre stores four booking reference numbers [8, 3, 6, 1] which must be sorted into ascending order using insertion sort.
FUNCTION InsertionSort(list)
  FOR i <- 1 TO LEN(list) - 1
    current <- list[i]
    j <- i - 1
    WHILE j >= 0 AND list[j] > current
      ______
      j <- j - 1
    ENDWHILE
    list[j + 1] <- current
  NEXT i
ENDFUNCTION
(a)Using the pseudocode above, explain how insertion sort builds up the sorted part of the list as i increases.(2)
(b)Complete the trace table below for InsertionSort([8, 3, 6, 1]), showing the value of current and the list contents at the end of each iteration of the outer FOR loop (i = 1, 2, 3).
icurrentlist at end of this iteration
1
2
3
(6)
(c)State the final sorted list produced.(1)
(d)Write the missing line of pseudocode (marked ______) needed inside the WHILE loop so the algorithm shifts elements correctly.(2)
(Total for Question 6 is 11 marks)
7
A playlist of 8 song lengths (in seconds) needs to be sorted using merge sort: [230, 180, 340, 95, 410, 150, 275, 60].
(a)Describe what happens during the divide stage and the merge (conquer) stage when merge sort sorts this playlist.(3)
(b)State how many times the playlist of 8 songs must be split before every sub-list contains a single element.(1)
(c)Explain why merge sort has a time complexity of O(n log n).(3)
(Total for Question 7 is 7 marks)
8
A library keeps an array titles of book titles sorted alphabetically. When a new book arrives, the system needs to find the position where the new title should be inserted so that titles stays in alphabetical order. If the title is already present, the function should return its existing index instead of inserting a duplicate.
(a)Write a function FindInsertPosition(titles, newTitle) RETURNS INTEGER, using a binary-search approach (rather than checking every element in turn), that:
- returns the index of newTitle if it is already present in titles, or
- otherwise returns the index at which newTitle should be inserted to keep the array sorted.

You may assume string comparisons (e.g. titles[mid] < newTitle) compare alphabetical order correctly. Use OCR pseudocode conventions. Marks are awarded for correct logic; minor syntax errors will be ignored.
(6)
(Total for Question 8 is 6 marks)
9
Each pseudocode fragment below processes a list of n items. All arrays are indexed from 0.
(a)Fragment A:
FOR i <- 0 TO n - 1
  OUTPUT list[i]
NEXT i

State the Big O time complexity of Fragment A in terms of n, and briefly justify your answer.
(2)
(b)Fragment B:
FOR i <- 0 TO n - 1
  FOR j <- 0 TO n - 1
    OUTPUT list[i] + list[j]
  NEXT j
NEXT i

State the Big O time complexity of Fragment B in terms of n, and briefly justify your answer.
(2)
(c)Fragment C:
count <- n
WHILE count > 1
  count <- count DIV 2
  OUTPUT count
ENDWHILE

State the Big O time complexity of Fragment C in terms of n, and briefly justify your answer.
(2)
(d)Fragment D:
FOR i <- 0 TO n - 1
  low <- 0
  high <- n - 1
  WHILE low <= high
    mid <- (low + high) DIV 2
    ...
  ENDWHILE
NEXT i

State the Big O time complexity of Fragment D in terms of n, and briefly justify your answer.
(2)
(Total for Question 9 is 8 marks)
10
The table below lists five algorithms covered in this topic.
(a)Complete the table below by writing the worst-case time complexity of each algorithm, using Big O notation.
AlgorithmWorst-case time complexity
Linear search
Binary search
Bubble sort
Insertion sort
Merge sort
(5)
(b)State, for each of bubble sort, insertion sort and merge sort, whether it requires significant additional (auxiliary) memory beyond the original list, or whether it can sort in place using only a small, constant amount of extra memory.(3)
(c)A shop assistant has a list of 20 till receipts that are already almost fully sorted by value, with only two receipts out of place. State, with a justification, which of bubble sort or insertion sort would typically perform fewer operations on this nearly-sorted list.(2)
(Total for Question 10 is 10 marks)
11
A supermarket's loyalty-card database holds 2,000,000 customer records, sorted by customer ID. Throughout the day, staff repeatedly need to search for a customer's record by ID. Every night, a batch of new customer records is added, and the whole file must then be back in ID order ready for the next day's searching.
(a)Discuss which searching algorithm and which sorting algorithm would be most appropriate for this system, justifying your recommendation by referring to the time complexity of the algorithms you have studied in this topic.(6)
(Total for Question 11 is 6 marks)
12
For merge sort, the maximum number of key comparisons C(n) needed to sort a list of n elements (where n is a power of 2) can be modelled by the recurrence relation:

C(n) = 2 x C(n / 2) + (n - 1), with C(1) = 0

This reflects sorting two half-lists (each needing C(n/2) comparisons) and then merging them together, which needs at most (n - 1) comparisons.
(a)Use the recurrence relation to calculate C(2) and C(4).(3)
(b)Hence show that C(8) = 17, giving the value of C(n) at each stage of your working.(2)
(c)State whether this result is consistent with merge sort having a time complexity of O(n log n), giving a reason.(1)
(Total for Question 12 is 6 marks)
Mark scheme · A2.3 Algorithms: Complexity, Searching and Sorting

Question 1

Question 2

Question 3

Question 4

Question 5

Question 6

Question 7

Question 8

Question 9

Question 10

Question 11

Question 12