57Easy
0/1

An earlier attempt at writing the bubble sort algorithm shown in Figure 1 had different code for lines 5 and 6.

Figure 1

1  data[0] ← 40
2  data[1] ← 30
3  data[2] ← 20
4  data[3] ← 10
5  FOR i ← 0 TO 2
6      FOR j ← 0 TO 2
7          IF data[j + 1] < data[j] THEN
8              temp ← data[j]
9              data[j] ← data[j + 1]
10             data[j + 1] ← temp
11         ENDIF
12     ENDFOR
13 ENDFOR

Lines 5 and 6 of the pseudo-code were originally written as:

5  FOR i ← 0 TO 3
6      FOR j ← 0 TO 3

Explain why the algorithm would result in an error when the value 3 was used instead of the value 2 on these two lines.

[1]

Sorting algorithms Questions

Practise AQA GCSE Computer Science Sorting algorithms with exam-style questions for GCSE Computer Science. 62 questions, matched to the AQA GCSE Computer Science (8525) 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

Sorting algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Sorting algorithms