{"base":{"id":"cs10-24","subject":"Algorithms","title":"Linear search and bubble sort","scene":"mix","steps":[{"use":"cs10t3","mode":"linear","eyebrow":"Linear search","title":"Check one by one","body":"Linear search starts at the first item and checks each in turn until it finds the target or reaches the end. Choose a target and step through.","controls":[{"id":"target","label":"Target (index)","min":0,"max":8,"step":1,"value":4}],"actions":[{"id":"step","label":"Check next"},{"id":"reset","label":"Reset"}]},{"use":"cs10t3","mode":"bubble","eyebrow":"Bubble sort","title":"Swap neighbours","body":"Bubble sort compares each pair of neighbours and swaps them if they are in the wrong order. After each pass the largest unsorted value reaches the end.","actions":[{"id":"step","label":"Compare"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"use":"code9","mode":"vslinear","eyebrow":"Efficiency","title":"Linear vs binary","body":"For a sorted list, binary search needs far fewer checks than linear search. Change the list size.","controls":[{"id":"n","label":"List size","min":10,"max":1000,"step":10,"value":100}]},{"use":"code9","mode":"maxcmp","eyebrow":"Choosing an algorithm","title":"One pass or sort first?","body":"To find the largest value, one pass is enough; sorting first would waste time. Change the list size.","controls":[{"id":"n","label":"List size","min":5,"max":100,"step":5,"value":20}]},{"use":"cs10t3","mode":"quiz","look":"bubble","eyebrow":"Check yourself","title":"One pass","body":"Bubble sort, ascending, on [4, 2, 5, 1].","question":"What is the list after the first full pass?","options":["[1, 2, 4, 5]","[2, 4, 1, 5]","[2, 1, 4, 5]"],"answer":1,"right":"Correct. 4>2 swap → [2,4,5,1]; 4<5 keep; 5>1 swap → [2,4,1,5].","wrong":"Not quite. One pass compares each neighbour pair once, left to right."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Step by step","items":[{"tag":"Linear search","title":"Pseudocode","body":"found ← FALSE\ni ← 0\nWHILE found = FALSE AND i < LENGTH(list)\n  IF list[i] = target THEN found ← TRUE\n  ELSE i ← i + 1\n  ENDIF\nENDWHILE","code":true},{"tag":"Bubble sort","title":"Pseudocode","body":"REPEAT\n  swapped ← FALSE\n  FOR i ← 0 TO LENGTH(list) − 2\n    IF list[i] > list[i+1] THEN\n      temp ← list[i]; list[i] ← list[i+1]; list[i+1] ← temp\n      swapped ← TRUE\n    ENDIF\n  NEXT i\nUNTIL swapped = FALSE","code":true},{"tag":"Trace","title":"[3, 1, 2]","body":"pass 1: [1,3,2] → [1,2,3] (2 swaps)\npass 2: no swaps → stop","code":true},{"tag":"Python","title":"Swap","body":"a[i], a[i+1] = a[i+1], a[i]","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Swapping without a temp variable","body":"In pseudocode, a ← b then b ← a loses a value. Use temp."},{"tag":"Mistake","title":"Looping one too far","body":"Compare i with i+1, so stop at LENGTH − 2."},{"tag":"Mistake","title":"Stopping after one pass","body":"Keep going until a pass makes no swaps."},{"tag":"Mistake","title":"Thinking linear search needs sorting","body":"Linear search works on unsorted lists; binary search needs sorted ones."}]},{"type":"bars","id":"data","eyebrow":"Data","title":"Worst-case checks","intro":"Linear search on n items may need n checks; binary search about log₂ n.","items":[{"label":"n = 10 linear","value":10,"display":"10","color":"#f08c2e"},{"label":"n = 10 binary","value":4,"display":"4","color":"#1f9d55"},{"label":"n = 1000 linear","value":1000,"display":"1000","color":"#f08c2e"},{"label":"n = 1000 binary","value":10,"display":"10","color":"#1f9d55"}],"facts":[{"title":"Bubble sort cost","body":"For n items, bubble sort can need about n²/2 comparisons: 4950 for 100 items."},{"title":"Faster sorts","body":"Merge sort needs about n log₂ n: around 700 comparisons for 100 items."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"Real world","title":"Sorting everywhere","body":"Phones sort contacts, shops sort prices, and search engines sort results. Real software uses faster algorithms like merge sort or quicksort."},{"tag":"History","title":"Why \"bubble\"?","body":"Large values \"bubble\" up to the end of the list like bubbles rising in water. The name appeared in the early 1960s."},{"tag":"Going further","title":"Insertion sort","body":"Insertion sort builds a sorted list one item at a time, like sorting playing cards in your hand. It is fast on nearly-sorted data."}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one on paper before opening the card.","items":[{"tag":"Challenge","title":"Trace","body":"How many swaps does bubble sort make on [5, 4, 3, 2, 1]?\n\nAnswer: 10 (every pair is out of order: 4 + 3 + 2 + 1)."},{"tag":"Challenge","title":"Code","body":"Write a Python linear search function that returns the index or −1.\n\nAnswer:\ndef find(lst, t):\n    for i in range(len(lst)):\n        if lst[i] == t:\n            return i\n    return -1","code":true},{"tag":"Challenge","title":"Improve","body":"How can bubble sort stop early?\n\nAnswer: track whether any swap happened in a pass; if none, the list is sorted."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Try it yourself","items":[{"title":"Human bubble sort","time":"15 minutes","need":["5–8 cards numbered randomly (or friends with numbers)"],"steps":["Lay the cards in a row.","Compare the first two; swap if the left is bigger. Move one step right and repeat.","Count passes and swaps until a pass has no swaps."],"why":"You executed bubble sort by hand and saw the largest values reach the end first.","safety":"None."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"Linear search checks items…","options":["from the middle","randomly","one at a time from the start"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"Linear search on 50 items: worst case checks =","options":["50","6","25"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"Bubble sort compares…","options":["the first and last items","neighbouring items","random pairs"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"After pass 1 of an ascending bubble sort, the largest item is…","options":["at the start","in the middle","at the end"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"Bubble sort can stop when…","options":["a pass has no swaps","one swap happens","the first item is smallest"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"Which search needs a sorted list?","options":["Linear","Binary","Neither"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"A temporary variable is used to…","options":["end a loop","store input","swap two values"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"[2, 1, 3] after one pass of bubble sort =","options":["[1, 2, 3]","[2, 1, 3]","[3, 2, 1]"],"answer":0,"right":"Correct.","wrong":"Not quite."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["describe and trace linear search","write linear search in pseudocode and Python","describe and trace bubble sort","write bubble sort with a swapped flag","compare the efficiency of searches","choose a suitable algorithm for a task"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"Linear search","meaning":"Checking each item in turn."},{"word":"Bubble sort","meaning":"Repeatedly swapping out-of-order neighbours."},{"word":"Pass","meaning":"One run through the list."},{"word":"Swap","meaning":"Exchanging two values."},{"word":"Efficiency","meaning":"How much time or memory an algorithm uses."}]},{"type":"sources","id":"sources","title":"Sources","items":["Raspberry Pi Foundation, Isaac Computer Science: searching and sorting","Khan Academy: algorithms","Owen Astrachan (2003), \"Bubble Sort: An Archaeological Algorithmic Analysis\"","BBC Bitesize, GCSE Computer Science: sorting algorithms"]}]},"overlay":null}