/en-tutor-prep-s5-term2-ict-algorithms-tracing-complexity/
/en-tutor-prep-s5-term2-ict-algorithms-tracing-complexity/
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

S5 Term 2 | ICT: Algorithm Tracing – Dictionaries, Sorting, Searching and Complexity | Tutor Lesson Plan | 45-minute flow, teaching script, common errors, practice with answers

S5 Term 2 | ICT: Algorithm Tracing – Dictionaries, Sorting, Searching and Complexity | Tutor Lesson Plan | 45-minute flow, teaching script, common errors, practice with answers

  • 2026-12-05

Lesson prep guide · S5 Term 2 ICT

Students see code and say they would just run it, but the paper gives no computer; asked how many comparisons, they guess a large number. This lesson teaches three things: hand tracing, comparison formulas, and complexity as the reason one algorithm beats another.

Lesson objectives Students can (1) trace dictionary accumulation to {2: 26, 1: 13} and a sum of 39; (2) use n(n − 1) over 2 to get 15 comparisons for six items and write the first bubble-sort pass; (3) explain that binary search needs 10 comparisons in 1,024 items while linear search needs 1,024, a factor of 102.4; (4) compare n squared with n log n, where 1,000 gives 1,000,000 against 10,000, or 100 times.

01 Prior knowledge check (first 3 minutes)

If any item below fails, fix it first, or this lesson stalls:

Skill Quick check Pass standard
Remainders 13 modulo 3 1
Powers 2 to the power 10 1,024
Logarithms log base 2 of 1,024 10
Triangle numbers 1 plus 2 plus 3 plus 4 plus 5 15

02 Lesson flow (45 minutes)

Time Stage Tutor does Student does
0–5 min Diagnostic Gives three lines of Python and asks for the output Most want to run it rather than trace it
5–17 min Dictionary trace n modulo 3 as key, accumulation as value, the get default Writes the dictionary after each line
17–29 min Bubble sort Each pass pushes the maximum right; 5+4+3+2+1 comparisons Writes the first pass
29–39 min Binary and linear search Halve the range, so 2 to the k must cover n Works out 65,536 items
39–43 min Complexity n squared against n log n when n doubles Explains why the gap grows
43–45 min Exit check Collects three answers, noting trace, formula or log errors Answers without notes

03 Teaching script (read it aloud)

Act one: trace it, do not run it

The rule of tracing is to write the variables after every iteration. For nums = [2, 5, 8, 11, 13], acc starts as an empty dictionary. Each n first gives a key of n modulo 3: 2, 2, 2, 2 and 1. The value is acc.get(key, 0) plus n, so the first 2 gives 2, the second adds 5 for 7, then 8 for 15, then 11 for 26, while 13 takes key 1, which is absent, so the default 0 plus 13 gives 13. The output is {2: 26, 1: 13} and sum(nums) is 39. With [1, 4, 7] every remainder is 1, so the result is {1: 12}.

Most common error: thinking the second argument of get adds zero. It is the default returned when the key is missing; without it Python raises KeyError.

Have students retrace with [1, 4, 7] and write the dictionary.

Students repeat: work out the key, look up the value, take 0 if it is missing, then add n.

Act two: bubble sort and comparison counts

Bubble sort compares neighbours from left to right on each pass and pushes the larger one right, so after one pass the maximum is last. Six items need five passes, with comparisons falling from five to one, so the total is 6 times 5 over 2, which is 15, or 5+4+3+2+1. For [45, 12, 78, 33, 9], 45 swaps with 12, 45 stays before 78, 78 swaps with 33, and 78 swaps with 9, so the first pass ends as [12, 45, 33, 9, 78] with 78 last. Selection and insertion sort also need n(n − 1) over 2 comparisons in the worst case, so saying bubble sort is always slowest is wrong.

Most common error: reporting five passes as five comparisons. Passes are n − 1 and comparisons are n(n − 1) over 2.

Have students write the comparison count of the second pass and check the total.

Students repeat: n items take n minus one passes and n times n minus one over two comparisons.

Act three: searching and complexity

Linear search checks every item, so 1,024 items take up to 1,024 comparisons. Binary search needs a sorted array and halves the range each time, so 10 comparisons leave one item, because 2 to the 10 is 1,024, which is 102.4 times fewer. For 65,536 items it takes 16 comparisons, and linear search takes 4,096 times as long. Merge sort splits and merges, costing about 1,024 times 10, or 10,240 comparisons, which is 51.15 times fewer than the 523,776 of bubble sort. Complexity is about growth: at n = 1,000, n squared is 1,000,000 against 10,000 for n log n, a factor of 100; at n = 2,000, n squared is 4,000,000, four times larger, while n log n is only 22,000, or 2.2 times. Have students explain why binary search needs sorting first.

Students repeat: binary halves and needs sorting, merge sort is n log n, and n squared loses badly as data grows.

04 Question types and steps

Type Example Steps Answer
Trace nums = [2, 5, 8, 11, 13] write key and value line by line, default 0 {2: 26, 1: 13}, sum 39
Sorting [45, 12, 78, 33, 9] first pass compare neighbours, push the larger right [12, 45, 33, 9, 78]
Search 1,024 sorted items 2 to the k must cover n 10 comparisons against 1,024
Complexity n = 1,000 n squared against n log n 1,000,000 against 10,000, or 100 times
1,024 items: linear 1,024 comparisons, binary 10, bubble 523,776, merge 10,240nums = [2, 5, 8, 11, 13]: acc[n modulo 3] += n gives {2: 26, 1: 13}, and sum is 39n = 2key 2n = 5key 2n = 8key 2n = 11key 2n = 13key 1{2: 26, 1: 13}2+5+8+11 under key 2; 13 under key 1with [1, 4, 7] every remainder is 1, giving {1: 12}comparisons (log length, labels give actual counts)Binary10Linear1,024Merge10,240Bubble523,776n(n − 1) ÷ 26 × 5 ÷ 2 = 152^k = n2^10 = 1,024n log2 n1,024 × 10 = 10,240Doubling from 1,000 to 2,000 takes n squared from 1,000,000 to 4,000,000, four times, while n log n only goes from 10,000 to 22,000, or 2.2 times

Tracing a dictionary needs three things: how the key is formed, what the value accumulates, and when the default 0 applies. Learn the three comparison formulas, and always state n and k.

Sanity check (must teach):Six items mean five passes and 15 comparisons, which are different numbers. 2 to the 10 is exactly 1,024, so binary search needs at most 10 comparisons, and 1,024 over 10 is 102.4, not 100. Merge sort at 10,240 comparisons is 51.15 times fewer than bubble sort at 523,776. At n = 1,000, n squared is 1,000,000 against 10,000, a factor of 100.

05 Three independent practice questions

Question Answer What to observe
Trace nums = [1, 4, 7] with the same code: what is printed? {1: 12} forgetting that all three remainders are 1
What is [45, 12, 78, 33, 9] after the first bubble pass? [12, 45, 33, 9, 78] stopping after one swap
How many comparisons does binary search need in 65,536 sorted items? 16 answering 32, which uses n rather than log base 2 of n

06 Common errors and correction lines

Error Student usually writes Tutor says
get treated as addition acc.get(n % 3) + n raises KeyError The second argument is the default 0 and cannot be dropped.
Passes counted as comparisons 5 given Five passes, but 5+4+3+2+1 is 15 comparisons.
Binary search on unsorted data binary search without sorting Binary search needs order to halve the range, or use linear search.
Complexity written as O(n) merge sort called O(n) Halving gives log n levels, each costing n, so n log n.

07 Homework (paper and question numbers)

08 Exit check (last 3 minutes)

Question Answer Verdict
How many comparisons does bubble sort need for six items in the worst case? 15, or 5+4+3+2+1 5 means act two must be retaught
How many comparisons does binary search need in 1,024 items? 10 1,024 means act three must be retaught
At n = 1,000, how far apart are n squared and n log n? 100 times, 1,000,000 against 10,000 10 times means act three must be retaught

09 Tutor FAQ

Which year is this page for?

S5 Term 2 algorithms and programming; S4 Python basics and S6 programming revision can start here.

Can three algorithms fit in 45 minutes?

Yes: 12 minutes on the dictionary trace, 12 on bubble sort, 10 on searching, 4 on complexity, and 7 for the diagnostic, practice and exit check.

Should recursion and quicksort be taught here?

No. This page covers tracing, bubble sort, binary search and merge sort; recursion and quicksort belong to another page.

Find a tutor freeTutor sign-up →

©2026 TutorZone · www.tutorzone.com.hk · Terms of use · All rights reserved. Original TutorZone material for personal study and teaching only; not for resale or republication.

Take this guide with you

Reading the full guide is free. Downloading the print version, printing it for students, or taking a whole-level pack needs a tutor account (free).

Registered tutors also get:

Register as a tutor — freeAlready registered? Log in

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →