An algorithm is shown below in pseudocode. It operates on an array named items containing three elements.
1 items ← [5, 2, 7]
2 sorted ← false
3 WHILE sorted = false
4 sorted ← true
5 idx ← 0
6 WHILE idx < 2
7 IF items[idx+1] > items[idx] THEN
8 temp ← items[idx]
9 items[idx] ← items[idx+1]
10 items[idx+1] ← temp
11 sorted ← false
12 ENDIF
13 idx ← idx + 1
14 ENDWHILE
15 ENDWHILE
Complete the trace table for the execution of this algorithm by identifying the values that should replace the letters (a), (b), (c), (d), (e), and (f).
sorted | idx | temp | items[0] | items[1] | items[2] |
|---|---|---|---|---|---|
| 5 | 2 | 7 | |||
| false | |||||
| true | 0 | ||||
| 1 | |||||
| 2 | 7 | 2 | |||
| (a) | 2 | ||||
| true | 0 | ||||
| (b) | (c) | 5 | |||
| (d) | 1 | ||||
| (e) | |||||
| true | 0 | ||||
| 1 | |||||
| (f) |
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.