Question bank › GCSE › GCSE Computer Science › Fundamentals of algorithms

Efficiency of algorithms

10 questions40 marksabout 50 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Grade 6–75 marks

Two algorithms both calculate the sum of the whole numbers from 1 to n.

Algorithm A

n ← STRING_TO_INT(USERINPUT)
total ← 0
FOR i ← 1 TO n
   total ← total + i
ENDFOR
OUTPUT total

Algorithm B

n ← STRING_TO_INT(USERINPUT)
total ← (n * (n + 1)) DIV 2
OUTPUT total

(a) State the output of both algorithms when the user enters 10. [1]

(b) Explain which algorithm is more efficient in terms of time. [2]

(c) A sorted list contains 1000 items. State the maximum number of items that would need to be checked against the search value to find an item (or show it is not there) using:

(i) a linear search [1]

(ii) a binary search. [1]

2Grade 1–33 marks

A programmer writes two different algorithms that both convert a temperature from Celsius to Fahrenheit.

(a) State one reason why it is possible for two different algorithms to solve the same problem. [1]

(b) The programmer says: "Algorithm 1 is more efficient than Algorithm 2."

Describe what is meant by one algorithm being more time efficient than another. [2]

3Grade 4–54 marks

Two algorithms both check whether the value in target is in the array nums. Both use the same array:

nums ← [12, 7, 25, 3, 18, 9, 14, 30]
target ← 3

Algorithm A

found ← False
FOR i ← 0 TO LEN(nums) - 1
   IF nums[i] = target THEN
      found ← True
   ENDIF
ENDFOR
OUTPUT found

Algorithm B

found ← False
i ← 0
WHILE i < LEN(nums) AND NOT found
   IF nums[i] = target THEN
      found ← True
   ENDIF
   i ← i + 1
ENDWHILE
OUTPUT found

(a) State how many times the comparison nums[i] = target is carried out in:

(i) Algorithm A [1]

(ii) Algorithm B. [1]

(b) Explain why Algorithm B is more time efficient than Algorithm A for this array. [2]

Stuck on Efficiency of algorithms?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Grade 4–54 marks

A developer times two algorithms, P and Q, that both remove duplicate entries from a list. Each algorithm is run on lists of different sizes and the times are recorded.

Number of items in listAlgorithm P time (ms)Algorithm Q time (ms)
10002.00.5
20004.02.0
40008.08.0

(a) Describe the pattern in the time taken by Algorithm P as the number of items doubles. [1]

(b) Describe the pattern in the time taken by Algorithm Q as the number of items doubles. [1]

(c) Estimate the time each algorithm would take for a list of 8000 items. [1]

(d) State which algorithm the developer should choose if the lists will normally contain around 50 000 items. Give a reason for your answer. [1]

5Grade 6–74 marks

Two algorithms both find the largest value in an array of whole numbers.

Algorithm A sorts the array into ascending order using a bubble sort and then outputs the last item.

Algorithm B is shown below.

largest ← nums[0]
FOR i ← 1 TO LEN(nums) - 1
   IF nums[i] > largest THEN
      largest ← nums[i]
   ENDIF
ENDFOR
OUTPUT largest

The array used is:

nums ← [14, 3, 27, 8, 19, 11]

(a) State the output of Algorithm B. [1]

(b) State how many comparisons between array items Algorithm B carries out for this array. [1]

(c) Explain why Algorithm A is less time efficient than Algorithm B. [2]

6Grade 6–75 marks

A program needs to check every pair of players in a tournament to see if they have already played each other. Two versions of the algorithm are written. In both, n is the number of players.

Version 1

count ← 0
FOR i ← 1 TO n
   FOR j ← 1 TO n
      count ← count + 1
   ENDFOR
ENDFOR
OUTPUT count

Version 2

count ← 0
FOR i ← 1 TO n
   FOR j ← i + 1 TO n
      count ← count + 1
   ENDFOR
ENDFOR
OUTPUT count

(a) State the output of Version 1 when n is 4. [1]

(b) State the output of Version 2 when n is 4. [1]

(c) When n is 100, Version 1 outputs 10 000. State the output of Version 2 when n is 100. [1]

(d) Explain why Version 2 is more time efficient than Version 1 while still checking every pair of players. [2]

7Grade 1–33 marks

The graph shows the number of steps carried out by two algorithms, X and Y, that both count how many items in a list are negative. The number of steps depends on the number of items in the list, n.

(a) Use the graph to state which algorithm carries out fewer steps when the list has 10 items. [1]

(b) State the number of items in the list for which both algorithms carry out the same number of steps. [1]

(c) State which algorithm is more time efficient for large lists. Give a reason for your answer. [1]

051015202530354004080120160200240280320Number of items in the list (n)Number of stepsAlgorithm XAlgorithm YAlgorithm X (solid)Algorithm Y (dashed)
8Grade 8–95 marks

A function checks whether a string is a palindrome (reads the same backwards as forwards). Two versions are written.

Version A builds a reversed copy of the whole string, then compares every character of the original with the same position in the reversed copy, setting pal to False if any pair differs.

Version B is shown below.

SUBROUTINE isPal(s)
   i ← 0
   j ← LEN(s) - 1
   pal ← True
   WHILE i < j AND pal
      IF s[i] ≠ s[j] THEN
         pal ← False
      ENDIF
      i ← i + 1
      j ← j - 1
   ENDWHILE
   RETURN pal
ENDSUBROUTINE

(a) State how many character comparisons (s[i] ≠ s[j]) Version B carries out when s is "racecar". [1]

(b) State how many character comparisons Version B carries out when s is "abcdefx". [1]

(c) For a string of 7 characters, Version A always carries out 7 steps to build the reversed string and then 7 comparisons.

Explain why Version B is more time efficient than Version A. [3]

9Grade 4–53 marks

An algorithm converts a list of times in days into seconds. The list days contains 250 values.

Algorithm 1

FOR i ← 0 TO LEN(days) - 1
   secs ← days[i] * 60 * 60 * 24
   OUTPUT secs
ENDFOR

Algorithm 2

perDay ← 60 * 60 * 24
FOR i ← 0 TO LEN(days) - 1
   secs ← days[i] * perDay
   OUTPUT secs
ENDFOR

(a) State the value stored in perDay in Algorithm 2. [1]

(b) Explain why Algorithm 2 is more time efficient than Algorithm 1, even though it has an extra line of code. [2]

10Grade 6–74 marks

A student is comparing two algorithms that both find out whether an array of n whole numbers contains any value more than once.

Algorithm 1 (5 lines of code) compares every item with every other item using two nested loops.

Algorithm 2 (12 lines of code) first sorts the array, then checks each item against the item next to it using a single loop.

The student says: "Algorithm 1 must be more efficient because it has fewer lines of code."

(a) Explain why the student's reasoning is incorrect. [2]

(b) Describe two ways the student could fairly compare the time efficiency of the two algorithms. [2]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session