Worked solution
(a) Trace table:
Initial TEMPS: [18, 22, 15, 25, 30, 12]
- Pass 1 (I = 0):
J runs from 0 to 4.
- J=0: TEMPS[0] (18) vs TEMPS[1] (22) -> no swap.
- J=1: TEMPS[1] (22) vs TEMPS[2] (15) -> swap -> [18, 15, 22, 25, 30, 12].
- J=2: TEMPS[2] (22) vs TEMPS[3] (25) -> no swap.
- J=3: TEMPS[3] (25) vs TEMPS[4] (30) -> no swap.
- J=4: TEMPS[4] (30) vs TEMPS[5] (12) -> swap -> [18, 15, 22, 25, 12, 30].
End of Pass 1 (I=0): TEMPS = [18, 15, 22, 25, 12, 30]
- Pass 2 (I = 1):
J runs from 0 to 3.
- J=0: 18 vs 15 -> swap -> [15, 18, 22, 25, 12, 30].
- J=1: 18 vs 22 -> no swap.
- J=2: 22 vs 25 -> no swap.
- J=3: 25 vs 12 -> swap -> [15, 18, 22, 12, 25, 30].
End of Pass 2 (I=1): TEMPS = [15, 18, 22, 12, 25, 30]
- Pass 3 (I = 2):
J runs from 0 to 2.
- J=0: 15 vs 18 -> no swap.
- J=1: 18 vs 22 -> no swap.
- J=2: 22 vs 12 -> swap -> [15, 18, 12, 22, 25, 30].
End of Pass 3 (I=2): TEMPS = [15, 18, 12, 22, 25, 30]
- Pass 4 (I = 3):
J runs from 0 to 1.
- J=0: 15 vs 18 -> no swap.
- J=1: 18 vs 12 -> swap -> [15, 12, 18, 22, 25, 30].
End of Pass 4 (I=3): TEMPS = [15, 12, 18, 22, 25, 30]
- Pass 5 (I = 4):
J runs from 0 to 0.
- J=0: 15 vs 12 -> swap -> [12, 15, 18, 22, 25, 30].
End of Pass 5 (I=4): TEMPS = [12, 15, 18, 22, 25, 30]
(b) Efficiency (Bubble Sort):
- Worst-case efficiency: \(O(N^2)\) because nested loops compare all elements regardless of initial state.
- Best-case efficiency: \(O(N^2)\) as written in this basic form because the loops execute fully even if the array starts sorted (Accept: \(O(N)\) if the candidate mentions that an optimized version uses a swap-tracking flag).
(c) Linear Search Pseudocode:
```
TARGET = input("Enter temperature: ")
FOUND = false
INDEX = -1
loop K from 0 to 5
if TEMPS[K] == TARGET then
FOUND = true
INDEX = K
end if
end loop
if FOUND == true then
output "Found at index ", INDEX
else
output "Not Found"
end if
```
Marking scheme
(a) [6 marks]
Award 1 mark for the correct initial state representation and 1 mark for each correct outer loop pass state:
- Initial state: [18, 22, 15, 25, 30, 12]
- End of I=0: [18, 15, 22, 25, 12, 30] (1 mark)
- End of I=1: [15, 18, 22, 12, 25, 30] (1 mark)
- End of I=2: [15, 18, 12, 22, 25, 30] (1 mark)
- End of I=3: [15, 12, 18, 22, 25, 30] (1 mark)
- End of I=4: [12, 15, 18, 22, 25, 30] (1 mark)
- 1 mark for structured presentation mapping variable state steps.
(b) [3 marks]
- 1 mark for identifying Worst-case as \(O(N^2)\).
- 1 mark for identifying Best-case as \(O(N^2)\) for basic implementation (or \(O(N)\) with clear justification of optimization flag).
- 1 mark for explaining why (e.g., nested loops always execute comparisons of adjacent values).
(c) [6 marks]
- 1 mark for taking input or initializing the search target correctly.
- 1 mark for initializing a boolean flag (e.g., FOUND = false) and/or index tracker.
- 1 mark for a loop traversing the array indices (0 to 5).
- 1 mark for comparing each array element to TARGET correctly.
- 1 mark for setting the flag to true and capturing the index when found.
- 1 mark for outputting the correct messages (Found with index, or Not Found) depending on the flag condition.