{"base":{"id":"cs8-08","subject":"Algorithms","title":"Linear search","scene":"search","steps":[{"mode":"play","eyebrow":"You vs the computer","title":"Find the name","body":"The names are face down. Flip cards to find the target, then let the computer search the same cards. Who needs fewer checks? Play a few rounds.","actions":[{"id":"run","label":"Let the computer try"},{"id":"new","label":"New round"}]},{"mode":"search","eyebrow":"Linear search","title":"One card at a time","body":"A linear search compares each item with the target, starting at position 0. Watch the code: the highlighted line is what the computer is doing right now.","actions":[{"id":"t0","label":"Find Nina"},{"id":"t1","label":"Find Ava"},{"id":"step","label":"One step"},{"id":"run","label":"Run it all"}],"targets":["Nina","Ava"]},{"mode":"search","eyebrow":"Not found","title":"When the item isn't there","body":"If the target isn't in the list, the search must check every card before it can say “not found”. Search for Tom, then for Sam, and count the checks.","actions":[{"id":"t0","label":"Find Tom"},{"id":"t1","label":"Find Sam"},{"id":"step","label":"One step"},{"id":"run","label":"Run it all"}],"targets":["Tom","Sam"]},{"mode":"size","eyebrow":"How long does it take?","title":"Bigger lists, more checks","body":"With a long list, a linear search may need many checks. The worst case is when the item is last or missing. Make the list longer and compare the bars.","controls":[{"id":"size","label":"Number of items in the list","min":10,"max":1000,"step":10,"value":100}]},{"mode":"quiz","eyebrow":"Check yourself","title":"Count the checks","body":"A list holds 20 names. A linear search looks for a name that is not in the list.","question":"How many comparisons does it make?","options":["1","10","20"],"answer":2,"right":"Correct. It has to check all 20 names before it can report “not found”.","wrong":"Not quite. Can it stop early if the name is missing?","look":"search"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"If it isn't found quickly, it isn't there.\"","body":"A linear search can only say “not found” after checking every item."},{"tag":"Popular idea","title":"\"The first item is at position 1.\"","body":"In most programming languages positions (indexes) start at 0."},{"tag":"Popular idea","title":"\"Searching is instant for a computer.\"","body":"Computers are fast, but a list of a million items could still need a million comparisons."}]},{"type":"cards","id":"uses","eyebrow":"Why search?","title":"Searches you use every day","items":[{"tag":"Usernames","title":"Is it taken?","body":"When you sign up, the system searches its list to check nobody already has your name."},{"tag":"Libraries","title":"Is the book here?","body":"A catalogue search finds the book and tells you which shelf it is on."},{"tag":"Shops","title":"In stock?","body":"A shop's website searches its stock list before letting you buy."}]},{"type":"cards","id":"algo","eyebrow":"The algorithm","title":"Linear search in pseudocode","items":[{"tag":"Pseudocode","title":"Search a list of 8 items","body":"INPUT target\ni ← 0\nWHILE i < 8 AND list[i] ≠ target\n    i ← i + 1\nENDWHILE\nIF i < 8 THEN\n    OUTPUT \"Found at position \", i\nELSE\n    OUTPUT \"Not found\"\nENDIF","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Card search","items":[{"title":"Find the card","time":"15 minutes","need":["A pack of playing cards (or 20 numbered paper slips)","A partner"],"steps":["Shuffle 20 cards and lay them face down in a row.","Your partner names a card. Turn the cards over one at a time from the left, counting each turn.","Stop when you find it, or when you reach the end.","Repeat five times and record the counts. Then try a card that you removed from the row."],"why":"This is a linear search. The count changes depending on where the card is, and a missing card always needs every turn.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"Why might a program need to search?","options":["To check whether an item is in stored data","To make the screen brighter","To connect to Wi-Fi"],"answer":0,"right":"Correct. Checking usernames or stock are examples.","wrong":"Not quite. Think about checking a username."},{"question":"Where does a linear search start?","options":["At the first item","In the middle","At a random item"],"answer":0,"right":"Correct. It works through the list in order.","wrong":"Not quite. Linear means in a line, from the start."},{"question":"When does a linear search stop?","options":["After 3 checks","Never","When the target is found or the list ends"],"answer":2,"right":"Correct. Those are the two ways it can finish.","wrong":"Not quite. There are two ways it can end."},{"question":"List: Leo, Mia, Kai. Searching for Kai. How many comparisons?","options":["1","2","3"],"answer":2,"right":"Correct. Leo, Mia, then Kai: three comparisons.","wrong":"Not quite. Count each item checked."},{"question":"A list doubles in length. What happens to the worst case?","options":["It halves","It doubles","It stays the same"],"answer":1,"right":"Correct. Every extra item may need checking.","wrong":"Not quite. The worst case checks every item."},{"question":"Which is an advantage of linear search?","options":["It is always the fastest method","It works even if the list isn't sorted","It never needs comparisons"],"answer":1,"right":"Correct. It doesn't need any order in the list.","wrong":"Not quite. Does it need the list to be sorted?"}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["why programs need to search stored data","how a linear search compares items one at a time","what happens when the target is found and when it is missing","why positions usually start at 0","how the list's length affects the number of checks"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"search","meaning":"Look for an item in stored data."},{"word":"target","meaning":"The item being searched for."},{"word":"linear search","meaning":"Checking items one after another from the start."},{"word":"index","meaning":"The position of an item in a list, usually counted from 0."},{"word":"comparison","meaning":"Checking whether two values are the same."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize KS3 Computing: Searching algorithms"]}]},"overlay":null}