Sorting algorithms

EasyMediumHard
12345678910111213141516171819202122
Question 9
Easy

An atmospheric research drone records six barometric pressure measurements at different altitudes and stores them in an array called pressure_readings.

A developer writes a bubble sort algorithm to sort these measurements in descending order:

1  pressure_readings[0] ← 1013.2
2  pressure_readings[1] ← 1008.5
3  pressure_readings[2] ← 1015.1
4  pressure_readings[3] ← 995.4
5  pressure_readings[4] ← 1002.3
6  pressure_readings[5] ← 991.0
7  FOR pass_idx ← 0 TO 4
8      FOR scan_idx ← 0 TO 4
9          IF pressure_readings[scan_idx + 1] > pressure_readings[scan_idx] THEN
10             temp ← pressure_readings[scan_idx]
11             pressure_readings[scan_idx] ← pressure_readings[scan_idx + 1]
12             pressure_readings[scan_idx + 1] ← temp
13         ENDIF
14     ENDFOR
15 ENDFOR

An earlier draft of this algorithm used the following code on line 8:

FOR scan_idx ← 0 TO 5

Explain why this change causes an error when the algorithm is executed.

[1]

Sorting algorithms Questions

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