Linear search scans the array from left to right, comparing each element to the target until it finds a match or exhausts the list. It works on any array — sorted or unsorted — but it is slow on large inputs because in the worst case every element must be checked. It is the baseline every faster search algorithm is measured against.
Example: Find 19 in an unsorted array of 8 numbers.
When to use this
→Search an unsorted array or linked list where sorting is not possible
→Find an element in a small list where simplicity matters more than speed
→Verify an element exists before more expensive processing
Linear search checks each element one by one from left to right. No sorting needed — we just walk the array until we find the target or run out of elements.
Space play/pause← → stepR replay+ − speed
active line highlighted
1
deflinear_search(arr,target):
2
foriinrange(len(arr)):
3
ifarr[i]==target:
4
returni
5
returni# not found placeholder
6
return-1
Quick check
Does linear search require the array to be sorted?
What is the worst-case number of comparisons for an array of n elements?