Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Searching and sorting algorithms

Standard searching algorithms: Linear search

All topicsPractise exam questions
Knowledge organiser

Searching and sorting algorithms

2.1.3b

Understand the main steps of each algorithm, Understand any pre-requisites of an algorithm, Apply the algorithm to a data set, Identify an algorithm if given the code or pseudocode for it

What you need to know

Linear search is a simple searching algorithm that checks each item in a list one by one, starting from the first item, until the target is found or the end of the list is reached. It works on both sorted and unsorted lists, making it versatile but slower than binary search for large datasets.

Key points

  • Linear search starts at the FIRST item and checks each item ONE BY ONE, IN ORDER.
  • It works on any list, whether sorted or unsorted — no pre-requisites needed.
  • The algorithm stops as soon as the target item is found to avoid unnecessary work.
  • If the item is not found after checking every element, the search returns False / -1 or 'not found'.
  • Simple to understand and implement.
  • Slow for large lists, especially if the item is near the end or not present at all.
  • Exam Tip:A common exam pattern is to iterate through an array using a for loop with the counter as the array index: theTeam[count].
  • Exam Tip:When describing a linear search for an item NOT in the list, say it checks all values in order from start to end — don't say 'until found' since it won't be found.
  • Exam Tip:'Checks each value' is not precise enough — you must state they are checked IN ORDER (sequentially).
  • Also called sequential search.

Code examples

Linear search in Python
python
def linear_search(data, target):
    for i in range(len(data)):
        if data[i] == target:
            return i  # Found at index i
    return -1  # Not found

numbers = [15, 7, 3, 5, 13, 8]
print(linear_search(numbers, 13))  # Output: 4
print(linear_search(numbers, 6))   # Output: -1