TutorChase logo
Login
AQA A-Level Computer Science

12.4.1 Linear Search

Linear search is a fundamental algorithm that checks each element in a list sequentially to find a target value. It is simple, direct, and widely used.

Linear search, also called sequential search, is one of the most basic and intuitive searching algorithms. It involves going through each item in a list, one by one, and comparing it to the target value until a match is found or the end of the list is reached.

It is called "linear" because the time it takes to complete the search increases linearly with the size of the list. For example, if a list has 100 elements, the algorithm may need to make up to 100 comparisons in the worst case.

This method is particularly useful in situations where:

  • The data is unsorted

  • The dataset is small

  • The search is only performed infrequently

Linear search is extremely easy to implement, and it works with all kinds of data, including numbers, strings, and more complex objects.

How linear search works

The process of linear search is straightforward and easy to follow. Here is a breakdown of how it operates:

  1. Start at the first element of the list.

  2. Compare the current element to the target value.

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

Practice Questions

FAQ

Linear search is typically implemented using iteration rather than recursion because of its straightforward nature and minimal need for additional overhead. Using recursion for linear search introduces unnecessary complexity and consumes more memory due to repeated function calls stored in the call stack. In most programming languages, especially those that do not support tail-call optimisation, each recursive call adds a new frame to the stack, which can lead to stack overflow for large datasets. Iterative linear search, on the other hand, uses a simple loop that checks each element without consuming extra memory, making it more efficient and easier to manage. Additionally, recursion adds little to no benefit in terms of clarity or performance for linear search, unlike other algorithms such as binary search or depth-first traversal, where recursion naturally mirrors the algorithm’s structure. Therefore, in practical and exam contexts, linear search is almost always expressed as an iterative algorithm for simplicity and efficiency.

In a standard linear search, the algorithm will return the first occurrence of the target value that it encounters while traversing the list. It begins at the first element and checks each one sequentially. As soon as it finds a match, it immediately stops and returns the corresponding index or position. It does not continue searching for any additional occurrences. This behaviour is important to understand, especially in applications where identifying all occurrences of a value is necessary. In such cases, a modified version of linear search would be required—one that does not stop at the first match but instead continues to search through the entire list and stores all the matching indices. However, this comes at the cost of additional time and memory usage. In exam conditions, unless stated otherwise, you should assume that linear search is designed to locate only the first matching element and ignore any duplicates that may exist afterwards.

While the core structure of linear search remains the same, there are certain optimisation techniques that can help improve performance in specific contexts without altering the algorithm entirely. One common method is ordering the list so that more frequently searched values appear earlier, thereby increasing the likelihood of finding the target sooner. Another technique is self-organising lists, where recently accessed elements are moved closer to the beginning of the list after being searched. This takes advantage of temporal locality, where recently used data is more likely to be used again soon. These enhancements don’t change the worst-case time complexity of O(n), but they can improve the average-case performance in real-world scenarios. Another optimisation is early termination in special cases, such as when a sentinel value can be introduced at the end of the list, reducing the need for an additional bounds check. However, these techniques depend heavily on the use case and data access patterns.

Linear search can be applied to complex data types such as objects, records, or structures, but it requires a well-defined comparison strategy. In basic data types like integers or strings, comparison is straightforward using equality operators. However, with complex data types, equality must be established by comparing one or more attributes. For example, when searching through a list of student objects, each with a name, ID, and score, the linear search must specify which field to compare—such as searching for a student by ID. In programming languages that support object-oriented features, this often involves implementing a custom equality method (e.g., equals() in Java or eq in Python) that defines when two objects are considered equal. If the comparison function is not properly defined, the search may fail to recognise matching objects, even if they are logically the same. Therefore, working with complex data in linear search demands careful attention to the equality criteria used during comparisons.

Despite its inefficiency for large datasets, linear search is often preferred in real-world scenarios where simplicity, flexibility, or one-time operations are more valuable than speed. For instance, in embedded systems with limited processing power and small datasets, linear search can be ideal because of its low memory requirements and minimal setup. In configuration file parsing or command-line argument scanning, where the list of options is short, linear search provides a quick and simple solution. It's also used in user interface elements, like dropdown lists, where the number of items is small and unlikely to change. Moreover, linear search is valuable in error handling routines or data validation checks, where readability and maintainability are prioritised over speed. In environments where data is accessed infrequently or never exceeds a few dozen elements, the cost of implementing and maintaining more complex algorithms outweighs the marginal performance gain, making linear search the practical choice.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email