22.1 What is Linear Search?
- Linear search checks each element in an array one by one, from the start to the end.
- Works on BOTH sorted and unsorted arrays — the order of values does not matter.
- Simple to implement but slow for large arrays.
- Stops when the target is found OR all elements have been checked.
- Time complexity:
O(n)— worst case checks all n elements.
Key idea: “Linear” means “in a line” — the algorithm walks through the array in a straight line from index 1 to index n. Also called sequential search for this reason.
Common mistake: Linear search is not the same as binary search. Binary search jumps to the middle of a sorted array and halves the search space each time — linear search just walks step by step.
What is Linear Search?
22.2 Linear Search Algorithm
The algorithm uses a Found flag (BOOLEAN) and a WHILE loop so it can exit early when the target is located.
- Step 1: Start at the first element (index 1).
- Step 2: Compare the current element with the search value.
- Step 3: If a match is found, report the position and stop.
- Step 4: If no match, move to the next element (
I <- I + 1). - Step 5: Repeat until found or end of array.
Example: search for a value in an array of 5 numbers
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE SearchVal : INTEGER
DECLARE I : INTEGER
DECLARE Found : BOOLEAN
Found <- FALSE
I <- 1
OUTPUT "Enter value to search: "
INPUT SearchVal
WHILE I <= 5 AND NOT Found DO
IF Numbers[I] = SearchVal THEN
Found <- TRUE
ELSE
I <- I + 1
ENDIF
ENDWHILE
IF Found THEN
OUTPUT "Found at position ", I
ELSE
OUTPUT "Not found"
ENDIFExam tip: The loop guard WHILE I <= 5 AND NOT Found DO does two jobs at once: it stops when I exceeds the array length (target not present) AND it stops the moment Found becomes TRUE (target located). This is the early-exit pattern examiners look for.
Linear Search Algorithm
22.3 Tracing a Linear Search
- A trace table records the value of key variables at each iteration.
- Typical columns:
I,Numbers[I],Found. - Each row in the table = one iteration of the loop.
- The trace ends when
Foundbecomes TRUE orIexceeds the array length.
Example: trace a search for 30 in [10, 20, 30, 40, 50]
| Iteration | I | Numbers[I] | Match? | Found |
|---|---|---|---|---|
| 1 | 1 | 10 | No (10 ≠ 30) | FALSE |
| 2 | 2 | 20 | No (20 ≠ 30) | FALSE |
| 3 | 3 | 30 | Yes (30 = 30) | TRUE → stop |
Output: "Found at position 3"
Common mistake: When the match is found, I does not increment — the loop exits immediately with I still at the matching index. If you add 1 to I before exiting, you will report the wrong position.
Tracing a Linear Search
22.4 Linear Search with FOR Loop
- An alternative implementation uses a
FORloop instead ofWHILE. - Simpler to write but always checks all elements (no early exit unless using a flag).
- Use the guard
AND NOT Foundto record only the first match. - A separate
Positionvariable is needed becauseIkeeps changing.
Example: linear search using a FOR loop
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE SearchVal : INTEGER
DECLARE I : INTEGER
DECLARE Found : BOOLEAN
DECLARE Position : INTEGER
Found <- FALSE
Position <- 0
OUTPUT "Enter value to search: "
INPUT SearchVal
FOR I <- 1 TO 5
IF Numbers[I] = SearchVal AND NOT Found THEN
Found <- TRUE
Position <- I
ENDIF
NEXT I
IF Found THEN
OUTPUT "Found at position ", Position
ELSE
OUTPUT "Not found"
ENDIFWHILE loop — can exit early
Found <- FALSE
I <- 1
WHILE I <= 5 AND NOT Found DO
IF Numbers[I] = SearchVal THEN
Found <- TRUE
ELSE
I <- I + 1
ENDIF
ENDWHILEFOR loop — always full scan
Found <- FALSE
Position <- 0
FOR I <- 1 TO 5
IF Numbers[I] = SearchVal AND NOT Found THEN
Found <- TRUE
Position <- I
ENDIF
NEXT IKey idea: The AND NOT Found guard inside the FOR loop prevents Position from being overwritten on later matches — so only the first occurrence is recorded. The loop itself still runs all 5 iterations because CIE pseudocode has no BREAK statement.
Linear Search with FOR Loop
22.5 Practical Examples
- Example 1: Search for a student name in a class list.
- Example 2: Find if a number exists in a lottery ticket array.
- Example 3: Search for a product by ID in an inventory array.
- Example 4: Count how many times a value appears (modified linear search).
Example 4: count how many times a value appears
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE SearchVal : INTEGER
DECLARE I : INTEGER
DECLARE Count : INTEGER
Count <- 0
OUTPUT "Enter value to count: "
INPUT SearchVal
FOR I <- 1 TO 5
IF Numbers[I] = SearchVal THEN
Count <- Count + 1
ENDIF
NEXT I
OUTPUT "Value appears ", Count, " time(s)"Exam tip: When a question asks you to count occurrences, remove the Found flag entirely — you want every match, not just the first. Replace it with a Count integer that increments on each match, and use a plain FOR loop with no early-exit guard.
Practical Examples
22.6 Key Points Summary
Linear search checks elements sequentially (one by one).
Works on sorted AND unsorted arrays.
Uses a Found flag (BOOLEAN) to track if found.
WHILE loop version can exit early when found.
FOR loop version checks all elements (unless using a flag).
Worst case: target at the end or not present (n comparisons).
Best case: target is the first element (1 comparison).
Position variable records WHERE the target was found.
Time complexity: O(n) in the worst case.
Linear search is an AS Level topic.
Exam tip: Whenever an exam question says “search an unsorted array” or “find whether a value is present”, reach for linear search with a Found flag. If it asks you to count occurrences, swap the flag for a Count integer and remove the early-exit guard.