Linear Search Examples in CS Thinking
Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Linear Search.
This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.
Concept Recap
A search algorithm that checks each element in a list one by one, starting from the first, until the target is found or the entire list has been examined. It is the simplest search algorithm and works on both sorted and unsorted data.
Looking for your keys by checking every pocket and drawer in order.
Read the full concept explanation →How to Use These Examples
- Read the first worked example with the solution open so the structure is clear.
- Try the practice problems before revealing each solution.
- Use the related concepts and background knowledge badges if you feel stuck.
What to Focus On
Core idea: Simple but slow for large lists — must check up to every element in the worst case.
Common stuck point: Linear search works on any list; binary search is faster but requires sorted data.
Sense of Study hint: To implement linear search, start at index 0 and compare each element to the target. If a match is found, return that index. If you reach the end without finding a match, return -1 or a 'not found' indicator. No sorting or special preparation is needed.
Worked Examples
Example 1
mediumfor i = 0 to n-1:
if A[i] == target: return i
return -1
```
for , target . Give the comparisons and return value.
Answer
First step
See the full worked solution + why-it-works coaching
SetupKey insightWhy it worksCommon pitfallConnection
Example 2
mediumExample 3
hardExample 4
hardExample 5
challengePractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
easyExample 2
easyExample 3
easyExample 4
easyExample 5
easyExample 6
easyExample 7
easyExample 8
easyExample 9
mediumExample 10
mediumExample 11
mediumExample 12
mediumExample 13
mediumExample 14
mediumExample 15
mediumExample 16
mediumExample 17
mediumExample 18
challengeExample 19
challengeExample 20
challengeExample 21
easyExample 22
easyExample 23
easyExample 24
easyExample 25
easyExample 26
easyExample 27
mediumExample 28
mediumExample 29
mediumExample 30
mediumExample 31
mediumExample 32
mediumExample 33
mediumExample 34
hardExample 35
hardExample 36
hardExample 37
hardExample 38
hardExample 39
challengeExample 40
challengeExample 41
challengeRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.