Sorting algorithms

EasyMediumHard
12345678910111213141516171819202122
Question 3
Easy

An indie game developer is writing a script to sort a leaderboard of the top 6 player scores in descending order using a bubble sort algorithm.

The developer's draft algorithm is shown below:

1  scores[0] ← 1420
2  scores[1] ← 890
3  scores[2] ← 2150
4  scores[3] ← 530
5  scores[4] ← 1760
6  scores[5] ← 1110
7  FOR i ← 0 TO 4
8      FOR j ← 0 TO 4
9          IF scores[j + 1] > scores[j] THEN
10             temp ← scores[j]
11             scores[j] ← scores[j + 1]
12             scores[j + 1] ← temp
13         ENDIF
14     ENDFOR
15 ENDFOR

In a previous version of this script, lines 7 and 8 were written as:

7      FOR i ← 0 TO 5
8      FOR j ← 0 TO 5

Explain why the script would trigger a runtime error if the value 5 was used instead of 4 on line 8.

[1]

Sorting algorithms Questions

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