/en-tutor-prep-ib-cs-programming-algorithms-big-o-search-sort-recursion/
/en-tutor-prep-ib-cs-programming-algorithms-big-o-search-sort-recursion/
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

IB Computer Science: Algorithms — Tutor Lesson Plan (Big O, linear and binary search, bubble and selection sort; binary finds 742 in 10 steps)

IB Computer Science: Algorithms — Tutor Lesson Plan (Big O, linear and binary search, bubble and selection sort; binary finds 742 in 10 steps)

  • 2026-11-28

Students can recite that binary search is O(log n) and then answer 512 when asked how many steps 1,024 names need. Algorithms are countable: the same lookup costs 743 linear steps and 10 binary ones.

Objectives — the student can (1) explain that Big O keeps only the leading term: bubble sort over 8 items makes 28 comparisons, so O(n squared), while binary search over 1,024 items needs at most 10, so O(log n); (2) trace both searches: in 1,024 sorted names, the 743th takes 743 linear steps and 10 binary ones, with midpoints 511, 767, 639 down to 742, a factor of 74.3; (3) state that binary search requires sorted data; (4) trace bubble sort on [45, 12, 78, 33, 56, 90, 21, 67]: 6 passes, 27 comparisons and 11 swaps, against 28 comparisons and 5 swaps for selection sort on the same data; (5) contrast them: bubble swaps neighbours and stops after 7 comparisons when the data is already ordered, while selection always makes 28 comparisons but swaps at most n minus one times; (6) trace recursion: factorial of 5 is 120 with a stack of 5 frames, and fib(5) makes 15 calls, with the base case and the memory cost named (HL).

01 Prerequisites (3-minute check)

If any of these fails, fix it before the lesson starts.

Skill Quick check Pass mark
Powers what is 2 to the power 10? 1,024
Midpoints what is the midpoint of indices 0 to 1,023? 511
Ordering of 45, 12 and 78, which is largest? 78

02 Lesson flow (60 minutes)

Time Stage Tutor does Student does
0-7 min Opening check how many steps for 1,024 names? write an estimate first
7-20 min Big O and three growth rates 1,024 items, 28 comparisons fill the growth table
20-34 min Linear and binary search the 743th: 743 against 10 trace the midpoints
34-48 min Bubble and selection sort eight numbers, 11 against 5 swaps trace each pass
48-57 min Recursion (HL) factorial 5 is 120, fib(5) makes 15 calls draw the call tree
57-60 min Exit check three questions marked on the spot work alone

03 Teaching script (read it out)

Scene 1: Big O: keep only the leading term

Open with a real problem: a library holds 1,024 sorted book numbers, how many comparisons find one? Most students halve it, because they are thinking of a linear search.
Explain what Big O is for: it names how the work grows as the input grows, not an exact count. Sorting 8 numbers takes 28 comparisons, which is 8 times 7 over 2; double the items to 16 and it becomes 120, four times as many, so the class is O(n squared). A linear search may look at all 1,024, O(n). Binary search halves the range each step, O(log n). Ask for the order from fast to slow, O(1), O(log n), O(n), O(n log n), O(n squared), with one example of O(1): reading item 500 of an array by index.
Give growth a feel with numbers. 1,000,000 operations at one microsecond each take 1 second. Grow the input from 1,000 to 10,000 and an O(n squared) algorithm needs 100,000,000 operations, or 100 seconds, so the same program goes from a second to a minute and forty. Ask what 100,000 would take: 10,000 seconds, nearly three hours. That answer is why algorithm choice is examined at all.
Space complexity takes two sentences: sorting in place uses a constant amount of extra memory, O(1); a second array the same size is O(n). Ask which class bubble and selection sort belong to: both are O(1), which is why they are still taught even though they are slow.

Student task: fill a five-row table (O(1), O(log n), O(n), O(n log n), O(n squared)) with one example from this chapter and the rough count at n equal to 1,000.

Scene 2: Searching: halve the range every step

Do linear search first. Among 1,024 sorted names, the 743th takes 743 comparisons from the front; the last would take 1024; the average is n plus 1 over 2, or 512.5. Ask what sorting does for linear search: nothing, which is its one strength. It is simple and works on any order.
Then binary search, writing every midpoint down. The range is 0 to 1023, so the first midpoint is 511; the target is larger, so the range becomes 512 to 1023 and the next midpoint 767; narrowing again gives 639, 703, 735, 751, 743, 739, 741 and 742, which hits, so 10 comparisons. Ask the class to count the halvings: 1,024 reaches 1 in 10 steps, which is why the worst case is 10 and the class is O(log n).
Two preconditions must be said out loud. First, the data must be sorted, or halving cuts away the half holding the answer. Second, the midpoint rule must stay consistent, rounding down or up, or a student midpoint sequence will not match the mark scheme.
Close with when each is used: a sorted list of names looking up a phone number uses binary; a phone number looking up a name must be linear, because numbers are not sorted. That is exactly the B2.4.2 requirement to choose a search by efficiency, and the answer needs both reasons, whether the data is sorted and how many lookups are made.

Student task: change the target to item 704, write the midpoint sequence again, count the comparisons, and say in one sentence how binary search fails on unsorted data.

Scene 3: Sorting and recursion: count the swaps and the calls

Trace bubble sort first. With [45, 12, 78, 33, 56, 90, 21, 67], the first pass pushes 78 to the end, the second pushes 90 to the second last, and so on. Run to completion: 6 passes, the sixth finding no swaps and stopping, 27 comparisons and 11 swaps. The theoretical maximum is 28 comparisons; 27 is lower because the last pass compares only the final pair. Ask why an ordered list needs one pass: 7 comparisons and 0 swaps prove it is ordered, so the loop can stop.
Then selection sort: each pass finds the smallest remaining item and moves it forward. Comparisons are always 28, because the scan happens regardless, but swaps are only 5, half the bubble figure. Ask for the trade-off: bubble is fast on nearly ordered data because it stops early, selection always pays 28 comparisons but makes the fewest swaps, which wins when a swap is expensive, for instance a database write.
Finish with recursion (HL). Factorial is n times factorial of n minus 1, with a base case returning 1 at n equal to 1. Factorial of 5 expands to 5 times 4 times 3 times 2 times 1, giving 120, with 5 frames on the stack at the deepest point. Ask what happens without a base case: the calls continue until the stack overflows, which Python reports as RecursionError and Java as StackOverflowError.
Then cost, with fib(5). Written straight from the definition it makes 15 calls for only 6 distinct inputs, so the same values are recomputed many times; memoising or rewriting it as a loop drops it to 5 calls. That pair of numbers is the evidence that recursion is compact but can be expensive. Ask for three good uses: tree traversal, quicksort and fractal images.

Student task: hand-run selection sort on three numbers, writing the index of the minimum and the array after each pass, then say why selection sort swaps at most n minus one times.

04 Question types and worked steps

Type Example Steps Answer
State the Big O class bubble sort over 8 items, binary search over 1,024 Three steps: count the worst-case work, keep the leading term and drop constants, write O(…) O(n squared) and O(log n)
Trace binary search 1,024 sorted names, target at position 743 Four steps: keep lo and hi, take the midpoint, move lo or hi after the comparison, repeat and count 10 comparisons, midpoints [511, 767, 639, 703, 735, 751, 743, 739, 741, 742]
Trace bubble sort [45, 12, 78, 33, 56, 90, 21, 67] Three steps: compare neighbours left to right, push the larger right, stop on a pass with no swaps 6 passes, 27 comparisons, 11 swaps
Compare bubble and selection the same 8 numbers Two questions: how many swaps, 11 against 5; how many passes when already ordered, 1 against seven bubble suits nearly ordered data, selection swaps least
Trace recursion (HL) factorial of 5 and fib(5) Three steps: write the base case, expand level by level, count calls and stack depth factorial 5 is 120 at depth 5; fib(5) makes 15 calls
Sanity check (teach this): Five sanity checks. (1) Comparisons never exceed n times n minus one over two: 8 items cap at 28, bubble actually makes 27 and selection 28, and neither may pass the cap. (2) Binary search must be far cheaper than linear: 10 against 743; an answer of 500 means each step is being treated as removing one item rather than half. (3) Sortedness is a precondition and must appear in the answer for full marks. (4) Swaps never outnumber comparisons: 11 below 27 and 5 below 28; reversed figures are wrong. (5) Recursive calls always outnumber the distinct inputs: factorial of 5 makes 5 calls for 120, fib(5) makes 15 for 5. The commonest misjudgement is reading O(log n) as “divide by log n”: 1,024 divided by 10 is 102.4, not 10; the right reading is how many halvings reach 1.

05 Independent practice (3 questions)

Question Answer What to watch
1,024 sorted names, looking for the 743th. (a) How many linear comparisons? (b) How many binary? (c) What is the ratio? (a) 743; (b) 10; (c) 74.3 times linear counts to the position, binary counts midpoints
Bubble sort on [45, 12, 78, 33, 56, 90, 21, 67]. (a) What is the maximum number of comparisons? (b) What are the actual comparison and swap counts? (a) 28; (b) 27 comparisons and 11 swaps the cap is n(n-1)/2 and the tail is already ordered
Trace factorial of 4. (a) The result? (b) The deepest stack? (c) What happens with no base case? (a) 24; (b) 4 frames; (c) it recurses until the stack overflows depth equals n and the base case returns at n equal to 1

06 Common mistakes and what to say

Mistake Student writes Say this
Reading O(log n) as divide by n answering that binary search over 1,024 takes 102 steps log n is the number of halvings, not a division by 10. 1,024 halves to 1 in 10 steps, so the worst case is 10. The answer 102 treats it as a tenth.
Binary search on unsorted data halving an unordered list halving assumes order: after comparing the midpoint you throw away the half that cannot contain the answer, and without sorting that half may hold it. Unsorted data needs a linear search, which is why databases build indexes.
Confusing swaps with comparisons answering 27 swaps a comparison looks at a pair, a swap moves a pair out of order. This data makes 27 comparisons but only 11 swaps, so read which one the question asks for.
A recursive function with no base case def f(n): return n * f(n – 1) with no stop without a base case the calls continue until the stack overflows. Every recursive function needs two sentences first: which case returns immediately, and how the rest shrinks the problem.

07 Homework (paper, section and question numbers)

08 Exit check (last 5 minutes)

Question Answer Decision
Big O the class of sorting 8 items and of binary search over 1,024 O(n) for both is wrong
Binary 1,024 names take at most 10 512 means it was treated as linear
Sorting bubble 11 swaps, selection 5 reversed figures mean swaps and comparisons are mixed

09 Tutor FAQ

How many Big O classes must be known?

Five is enough: O(1), O(log n), O(n), O(n log n) and O(n squared). This chapter needs linear search O(n), binary O(log n), and bubble and selection O(n squared); for O(n log n) it is enough to know merge sort and quicksort belong there.

Is recursion HL only?

B2.4.4 and B2.4.5 are marked HL only, so SL students only need to know recursion exists. Big O, both searches and both sorts, B2.4.1 to B2.4.3, carry no HL marker and apply to everyone.

Which sort is better?

There is no absolute answer, only conditions. Nearly ordered data suits bubble, which stops after 7 comparisons; expensive swaps suit selection, which makes only 5. Both are O(n squared) at worst, so large data needs an O(n log n) algorithm.

Find a tutor — freeTutor Registration →

A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.

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