Stewdent.eu
▾

Linear Search

Linear search checks each item in a list one by one until it finds the target or reaches the end. It is the simplest search algorithm and works on any list, sorted or unsorted.

Last updated: 20 March 2026

★☆☆☆☆ Level 1computingalgorithmssearchinglinear search4 min read

Report an error

Imagine looking for a specific book on a disorganised shelf. You start from one end and check each book in turn until you find the one you want — or reach the end without finding it. This is exactly how linear search works: it checks items one by one from start to finish.

How Linear Search Works

  1. Start at the first item in the list.
  2. Compare it with the target value.
  3. If they match — found it! Return the position.
  4. If not — move to the next item.
  5. If you reach the end without a match — the item is not in the list.

Python Implementation

def linear_search(items, target):
    for i in range(len(items)):
        if items[i] == target:
            return i       # return the index where target was found
    return -1              # -1 indicates "not found"

fruits = ["banana", "apple", "cherry", "mango"]
result = linear_search(fruits, "cherry")
print(result)    # 2

result = linear_search(fruits, "grape")
print(result)    # -1
Note

Returning -1 for "not found" is a common convention. Always check the result before using it as an index! Some implementations return None instead, which can also be useful.

A Shorter Python Version

Python's in operator and index() method perform linear searches automatically:

fruits = ["banana", "apple", "cherry", "mango"]

if "cherry" in fruits:
    print("Found at index:", fruits.index("cherry"))

Under the hood, this is doing the same thing — checking each item until a match is found.

Best and Worst Cases

ScenarioComparisons needed
Target is the first item1
Target is the last itemn
Target is not in the listn
Average casen / 2

For a list of n items, linear search checks at most n items. We describe this as O(n) time complexity — doubling the list size roughly doubles the time.

Tip

Linear search is perfectly fine for small lists (under a few hundred items). It only becomes a problem for very large datasets where speed matters.

Advantages and Disadvantages

AdvantageDisadvantage
Works on unsorted listsSlow for large lists
Simple to implementChecks every item until target found
Works on any data typeO(n) time complexity
Warning

If you have a sorted list and you need to search it frequently, consider binary search instead. Binary search is far more efficient for large sorted lists, but linear search is your only option if the list is unsorted.

A Practical Example

def find_student(students, name):
    for index, student in enumerate(students):
        if student.lower() == name.lower():
            return index
    return -1

register = ["Alice", "Bob", "Carlos", "Diana", "Eve"]
pos = find_student(register, "carlos")
if pos != -1:
    print(f"Found at position {pos}")
else:
    print("Student not found")

Practice Problems

In the worst case, how many comparisons does linear search make on a list of 20 items? When does the worst case occur?

Show answer
20 comparisons. The worst case occurs either when the target item is the last item in the list, or when the target is not in the list at all — the algorithm must check every single item before it can report the result.

What does linear search return if the target is not found in the list (using the implementation shown above)?

Show answer
-1. This is a conventional sentinel value meaning 'not found'. The caller should always check whether the result is -1 before trying to use it as a list index.