{"base":{"id":"cs9-12","subject":"Algorithms","title":"Binary search","scene":"code9","steps":[{"mode":"binary","eyebrow":"Halve it","title":"Check the middle","body":"In a sorted list, check the middle item. If it's too small, discard the left half; if too big, discard the right half. Repeat. Pick a target and step through.","actions":[{"id":"t0","label":"Find 42"},{"id":"t1","label":"Find 7"},{"id":"t2","label":"Find 30"},{"id":"step","label":"Step"}]},{"mode":"vslinear","eyebrow":"How fast?","title":"Linear vs binary","body":"Linear search might check every item. Binary search halves the list each time. Change the list size and compare the worst cases.","controls":[{"id":"n","label":"List size","min":10,"max":1000,"step":10,"value":100}]},{"mode":"unsorted","eyebrow":"Why sorted?","title":"Binary search on a jumbled list","body":"Binary search assumes the list is in order. Try it on an unsorted list where 51 really is present. Step through and see what goes wrong.","actions":[{"id":"step","label":"Step"},{"id":"reset","label":"Reset"}]},{"mode":"quiz","eyebrow":"Check yourself","title":"Checks","body":"A sorted list has 31 items.","question":"What is the most checks binary search needs?","options":["31","5","16"],"answer":1,"right":"Correct. 31 → 15 → 7 → 3 → 1: at most 5 checks.","wrong":"Not quite. Keep halving.","look":"vslinear"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"Binary search works on any list.\"","body":"The list must be sorted, or it can discard the half containing the target."},{"tag":"Popular idea","title":"\"Binary search always finds the item faster.\"","body":"If the target is first in the list, linear search finds it in one check."},{"tag":"Popular idea","title":"\"Binary means it only works with 0s and 1s.\"","body":"Here 'binary' means splitting into two halves."}]},{"type":"cards","id":"code","eyebrow":"Pseudocode","title":"Binary search","items":[{"tag":"Algorithm","title":"Sorted list","body":"low ← 0; high ← length − 1\nWHILE low <= high\n    mid ← (low + high) DIV 2\n    IF list[mid] = target THEN RETURN mid\n    IF list[mid] < target THEN low ← mid + 1\n    ELSE high ← mid − 1\nENDWHILE\nRETURN −1   // not found","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Guess my number","items":[{"title":"Halving game","time":"10 minutes","need":["A partner"],"steps":["Your partner thinks of a number from 1 to 100.","Always guess the middle of what's left; they say 'higher' or 'lower'.","Count your guesses.","Can you always do it in 7 guesses or fewer? Why?"],"why":"Each guess halves the possibilities: 100 → 50 → 25 → 13 → 7 → 4 → 2 → 1.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"What must be true before using binary search?","options":["The list is short","The list is sorted","The list has even length"],"answer":1,"right":"Correct. Order lets it discard halves.","wrong":"Not quite. What does it rely on?"},{"question":"Binary search checks first…","options":["the middle item","the first item","the last item"],"answer":0,"right":"Correct. Then discards a half.","wrong":"Not quite. Where does it start?"},{"question":"The middle item is too big. What next?","options":["Search the right half","Search the left half","Start again"],"answer":1,"right":"Correct. Bigger values are to the right.","wrong":"Not quite. Discard the bigger side."},{"question":"About how many checks for 1000 sorted items (worst case)?","options":["500","1000","10"],"answer":2,"right":"Correct. 2¹⁰ = 1024.","wrong":"Not quite. How many halvings?"},{"question":"When is linear search better?","options":["The list is huge and sorted","Never","The list is unsorted"],"answer":2,"right":"Correct. Binary search needs sorting.","wrong":"Not quite. What does binary search need?"},{"question":"If low > high, binary search…","options":["has not found the item","has found it","must restart"],"answer":0,"right":"Correct. Nothing is left to check.","wrong":"Not quite. The search interval is empty."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["how binary search halves the search interval","why the list must be sorted","how to compare linear and binary search","when binary search reports 'not found'"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"binary search","meaning":"Searching a sorted list by repeatedly checking the middle."},{"word":"linear search","meaning":"Checking items one by one."},{"word":"sorted","meaning":"In order."},{"word":"midpoint","meaning":"The middle position of the current interval."},{"word":"interval","meaning":"The part of the list still being searched."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize KS3 Computer Science","Raspberry Pi Foundation, Computing curriculum resources"]}]},"overlay":null}