21
0/1

An algorithm is designed to sort a 2D array of runners by their marathon times in ascending order.

1 # runners has the format [runnerID, name, raceTime]
2
3 PROCEDURE bubbleSortRunners()
4     SET temp TO []
5     FOR i FROM 0 TO LENGTH(runners) - 1 DO
6         FOR j FROM 0 TO LENGTH(runners) - 1 DO
7             IF runners[j, 2] > runners[j + 1, 2] THEN
8                 SET temp TO runners[j]
9                 SET runners[j] TO runners[j + 1]
10                SET runners[j + 1] TO temp
11            END IF
12        END FOR
13    END FOR
14 END PROCEDURE

The algorithm currently contains an error on line 6 that causes an 'index out of bounds' error when it is run.

Give a new line of code that will correct the error on line 6.

[1]

Develop code Questions

Practise Edexcel GCSE Computer Science Develop code with exam-style questions for GCSE Computer Science. 57 questions covering Decomposition and abstraction to solve problems, Read, write, analyse and refine programs, Converting algorithms into programs, Techniques for readable, maintainable code, Identifying and correcting program errors, and Evaluating program fitness and efficiency, matched to the Edexcel GCSE Computer Science (1CP2) specification and written in Paper 1 and Paper 2 style. Every question includes a full worked solution and mark scheme, so you can see where marks are awarded rather than just whether you got the answer right.

PreviousNext

Develop code Questions

  1. GCSE
  2. /Computer Science
  3. /Develop code