Skip to content

Course home

Sorting algorithms

Sorting algorithms

EasyMediumHard
12345678910111213141516171819202122
Question 22

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]
Markscheme

Sorting algorithms Questions

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

52 exam-style questions on AQA GCSE Computer Science Sorting algorithms. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank