A microcontroller in a smart thermostat sorts temperature readings to help analyze temperature ranges. The following algorithm is designed to sort three sensor readings stored in the array temps in ascending order.
1 temps ← [4, 9, 3]
2 sorted ← false
3 WHILE sorted = false
4 sorted ← true
5 i ← 0
6 WHILE i < 2
7 IF temps[i+1] < temps[i] THEN
8 temp ← temps[i]
9 temps[i] ← temps[i+1]
10 temps[i+1] ← temp
11 sorted ← false
12 ENDIF
13 i ← i + 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 | i | temp | temps[0] | temps[1] | temps[2] |
|---|---|---|---|---|---|
| 4 | 9 | 3 | |||
| false | |||||
| true | 0 | ||||
| 1 | |||||
| 9 | 3 | 9 | |||
| (a) | 2 | ||||
| true | 0 | ||||
| (b) | (c) | 4 | |||
| (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.