Linear Search algorithm in pseudocode

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

Linear Search algorithm in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does linear search check elements one at a time from the start?
A Yes
B No
2. Does linear search require the array to be sorted first?
A Yes
B No
3. Does the FOR loop in linear search typically start at index 1?
A No
B Yes
4. Is == the correct comparison operator in this pseudocode?
A No
B Yes
5. Can linear search stop checking once the target is found?
A Yes
B No

Match the Pairs

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

Left columnRight column
1FOR i FROM 1 TO 5 Afound ← TRUE
2IF numbers[i] = target THEN BDISPLAY found
3Records that a match has been found CLoops through each index of the array
4Outputs a message or value to the user DWrong - pseudocode array loops start from 1, not 0
5IF numbers(i) = target THEN EWrong - array elements use square brackets, not round
6FOR i FROM 0 TO 5 FChecks whether the current element matches the target

Definitions

Linear Search

Fill in the Blanks

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

1.
___ i FROM 1 TO 5
2.
__ numbers[i] = target THEN
3.
IF numbers[i] = target ____
4.
_______ "Found at position", i
5.
IF numbers[i] _ target THEN

Spot the Error

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

Question 1
A DECLARE numbers(5) OF INTEGER
B DECLARE target OF INTEGER
C DECLARE found OF BOOLEAN
Question 2
A FOR i FROM 1 TO 5
B IF numbers[i] = target THEN
C END WHILE
Question 3
A FOR i FROM 0 TO 5
B IF numbers[i] = target THEN
C END IF
Question 4
A FOR i FROM 1 TO 5
B IF numbers[i] == target THEN
C found ← TRUE
D END IF
Question 5
A FOR i FROM 1 TO 5
B IF numbers[i] = target THEN
C found = TRUE
D END IF

Answer Key

Linear Search algorithm in pseudocode
Section A: Quick Questions
Q1
A — Yes
Q2
B — No
Q3
B — Yes
Q4
A — No
Q5
A — Yes
Section B: Match the Pairs
Q1
FOR i FROM 1 TO 5 → Loops through each index of the array
Q2
IF numbers[i] = target THEN → Checks whether the current element matches the target
Q3
Records that a match has been found → found ← TRUE
Q4
Outputs a message or value to the user → DISPLAY found
Q5
IF numbers(i) = target THEN → Wrong - array elements use square brackets, not round
Q6
FOR i FROM 0 TO 5 → Wrong - pseudocode array loops start from 1, not 0
Section C: Definitions
Q1
Linear Search — An algorithm that checks each element in an array one at a time, from the start, until the target is found or the end is reached.
Section D: Fill in the Blanks
Q1
FOR i FROM 1 TO 5
Q2
IF numbers[i] = target THEN
Q3
IF numbers[i] = target THEN
Q4
DISPLAY "Found at position", i
Q5
IF numbers[i] = target THEN
Section E: Spot the Error
Q1
A — Array declarations use square brackets, not round brackets.
Q2
C — An IF block must close with END IF, not END WHILE.
Q3
A — Pseudocode array loops start from 1, not 0, by convention.
Q4
B — Pseudocode comparison uses a single =, not ==.
Q5
C — Assignment inside pseudocode uses ←, not =.