Linear search: how it works

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Linear search: how it works

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A linear search checks the items of a list in which order?
A One at a time, from the start of the list
B Starting from the middle of the list
2. Can a linear search be used on a list that is NOT sorted?
A No, the list must be sorted first
B Yes, it works on unsorted data
3. A linear search is looking for the target 7 in the list [4, 9, 7, 2]. How many items are checked before 7 is found?
A 3
B 4
4. A linear search looks for the target 5 in [8, 3, 6, 1]. What is the result?
A Not found, after checking all 4 items
B Not found, after checking only the first item
5. What is the BEST case for a linear search?
A The target is the last item, so only 1 check is needed
B The target is the first item, so only 1 check is needed

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1FOR Index ← 1 TO 10 IF Names[Index] = Target THEN Found ← TRUE AThe average case is about half the list, around 50 checks. Only the best case needs 1 check.
2Found ← FALSE BFOR Index ← 1 TO 10
3Best case: the target is the very first item, so only 1 check is needed CChecks each item in the list in turn, starting from the first, to see if it equals the target
4Worst case: the target is the last item or not in the list, so every item is checked DA linear search works on unsorted data. It simply checks each item in turn, so no sorting is needed.
5Linear search needs the list to be sorted into order first, otherwise it cannot work EStarts the search with the target not yet found; it only becomes TRUE if a matching item turns up
6On average, a linear search of 100 items needs 1 check to find the target FNames[1] = Target

Definitions

linear search

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Target ← 7 Index ← 0 Found ← _____
2.
// Scores has 5 items (index 0 to 4) FOR Index ← 0 TO _
3.
FOR Index ← 0 TO 4 IF Scores[Index] _ Target THEN Found ← TRUE
4.
IF Scores[Index] = Target THEN Found ← ____ ENDIF
5.
// Worst case: target is last or missing // List has 5 items, so most checks is: Checks ← _

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Found ← TRUE
B FOR I ← 1 TO 10
C IF List[I] = Target THEN
D Found ← TRUE
E ENDIF
F NEXT I
Question 2
A Found ← FALSE
B FOR I ← 1 TO 9
C IF List[I] = Target THEN
D Found ← TRUE
E ENDIF
F NEXT I
Question 3
A Found ← FALSE
B FOR I ← 1 TO 10
C IF List[I] ← Target THEN
D Found ← TRUE
E ENDIF
F NEXT I
Question 4
A Found ← FALSE
B FOR I ← 1 TO 10
C IF List[I] = Target THEN
D Found ← FALSE
E ENDIF
F NEXT I
Question 5
A Position ← 0
B FOR I ← 1 TO 10
C IF List[I] = Target THEN
D Position ← Target
E ENDIF
F NEXT I

Answer Key

Linear search: how it works
Section A: Quick Questions
Q1
A — One at a time, from the start of the list
Q2
B — Yes, it works on unsorted data
Q3
A — 3
Q4
A — Not found, after checking all 4 items
Q5
B — The target is the first item, so only 1 check is needed
Section B: Match the Pairs
Q1
FOR Index ← 1 TO 10 IF Names[Index] = Target THEN Found ← TRUE → Checks each item in the list in turn, starting from the first, to see if it equals the target
Q2
Found ← FALSE → Starts the search with the target not yet found; it only becomes TRUE if a matching item turns up
Q3
Best case: the target is the very first item, so only 1 check is needed → Names[1] = Target
Q4
Worst case: the target is the last item or not in the list, so every item is checked → FOR Index ← 1 TO 10
Q5
Linear search needs the list to be sorted into order first, otherwise it cannot work → A linear search works on unsorted data. It simply checks each item in turn, so no sorting is needed.
Q6
On average, a linear search of 100 items needs 1 check to find the target → The average case is about half the list, around 50 checks. Only the best case needs 1 check.
Section C: Definitions
Q1
linear search — Checks each item in a list in turn, from the start, until the target is found or the end of the list is reached. It works on unsorted data.
Section D: Fill in the Blanks
Q1
Target ← 7 Index ← 0 Found ← FALSE
Q2
// Scores has 5 items (index 0 to 4) FOR Index ← 0 TO 4
Q3
FOR Index ← 0 TO 4 IF Scores[Index] = Target THEN Found ← TRUE
Q4
IF Scores[Index] = Target THEN Found ← TRUE ENDIF
Q5
// Worst case: target is last or missing // List has 5 items, so most checks is: Checks ← 5
Section E: Spot the Error
Q1
A — The flag must start as FALSE because the target has not been found yet. If it starts as TRUE, the search would report found even when the target is not in the list. It should be Found ← FALSE.
Q2
B — The list has 10 items, so the loop must check every one, from 1 to 10. Stopping at 9 means the last item is never checked. It should be FOR I ← 1 TO 10.
Q3
C — A search compares each item with the target, so it needs the comparison operator =. The arrow ← is for assignment. It should be IF List[I] = Target THEN.
Q4
D — When the current item matches the target, the flag must be set to TRUE to record that the target was found. Setting it to FALSE means the match is never recorded. It should be Found ← TRUE.
Q5
D — The position of a match is the index of the item, which is the loop counter I. Storing Target just copies the value being searched for. It should be Position ← I.