Sorting algorithms

EasyMediumHard
12345678910111213141516171819202122
Question 6
Easy

A student is writing an algorithm to sort an array of 4 integers using a bubble sort.

1  items[0] ← 45
2  items[1] ← 12
3  items[2] ← 89
4  items[3] ← 23
5  FOR out_loop ← 0 TO 2
6      FOR in_loop ← 0 TO 2
7          IF items[in_loop + 1] < items[in_loop] THEN
8              temp ← items[in_loop]
9              items[in_loop] ← items[in_loop + 1]
10             items[in_loop + 1] ← temp
11         ENDIF
12     ENDFOR
13 ENDFOR

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

FOR in_loop ← 0 TO 3

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

[1]

Sorting algorithms Questions

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