Photo AI

Last Updated Sep 27, 2025

Linear Search Simplified Revision Notes

Revision notes with simplified explanations to understand Linear Search quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

340+ students studying

Linear Search

Overview

Linear Search is a simple algorithm used to find the position of a target value in a list. It works by sequentially checking each element of the list until the target is found or the end of the list is reached. Linear Search is easy to implement but can be inefficient for large datasets.

How Linear Search Works

  • Process:
    1. Start at the first element of the list.
    2. Compare each element with the target value.
    3. If a match is found, return the position of the element.
    4. If the end of the list is reached without finding the target, return an indication that the target is not in the list.
  • Applicable to:
    • Both unsorted and sorted datasets.

Time Complexity

  • Best Case: O(1) (target is the first element).
  • Worst Case: O(n) (target is the last element or not present).
  • Average Case: O(n/2), simplified to O(n).

Space Complexity

  • O(1), as no extra memory is required beyond the input list.
infoNote

Example: Linear Search

Given the array:

3, 7, 10, 15, 18, 20

Target: 15

Step-by-Step Execution:

  1. Compare 3 with 15 → Not a match.
  2. Compare 7 with 15 → Not a match.
  3. Compare 10 with 15 → Not a match.
  4. Compare 15 with 15 → Match found. Position: Index 3.

Pseudocode for Linear Search

METHOD LinearSearch(array, target)
    FOR i FROM 0 TO array.length - 1
        IF array[i] = target
            RETURN i  // Target found at index i
    ENDFOR
    RETURN -1  // Target not found

Python Example

def linear_search(array, target):
    for i in range(len(array)):
        if array[i] == target:
            return i  # Target found
    return -1  # Target not found

# Example usage:
data = [3, 7, 10, 15, 18, 20]
print(linear_search(data, 15))  # Output: 3
print(linear_search(data, 5))   # Output: -1

Tracing Linear Search

infoNote

Example

Given the array:

2, 5, 8, 12, 16, 23, 38

Target: 23

  1. Iteration 1: Compare 2 with 23 → No match.
  2. Iteration 2: Compare 5 with 23 → No match.
  3. Iteration 3: Compare 8 with 23 → No match.
  4. Iteration 4: Compare 12 with 23 → No match.
  5. Iteration 5: Compare 16 with 23 → No match.
  6. Iteration 6: Compare 23 with 23 → Match found. Output: Index 5.

Note Summary

infoNote

Common Mistakes

  1. Not Handling Target Not Found: Forgetting to return a special value (e.g., -1) when the target is not in the list.
  2. Index Errors: Incorrect loop bounds can cause out-of-range errors.
  3. Confusion Between Return and Print: Using print instead of return inside the function can lead to incorrect handling of results in larger programs.
  4. Inefficient for Large Datasets: Using Linear Search on very large lists when the data could be sorted and searched more efficiently with algorithms like Binary Search.
infoNote

Key Takeaways

  • Linear Search is a simple and versatile algorithm that works on both sorted and unsorted datasets.
  • It has a time complexity of O(n), making it inefficient for large datasets.
  • The algorithm is straightforward to implement and understand.
  • Ensure proper handling of cases where the target value is not found.
Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Linear Search

Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!

120 flashcards

Flashcards on Linear Search

Revise key concepts with interactive flashcards.

Try Computer Science Flashcards

12 quizzes

Quizzes on Linear Search

Test your knowledge with fun and engaging quizzes.

Try Computer Science Quizzes

29 questions

Exam questions on Linear Search

Boost your confidence with real exam questions.

Try Computer Science Questions

27 exams created

Exam Builder on Linear Search

Create custom exams across topics for better practice!

Try Computer Science exam builder

12 papers

Past Papers on Linear Search

Practice past papers to reinforce exam experience.

Try Computer Science Past Papers

Other Revision Notes related to Linear Search you should explore

Discover More Revision Notes Related to Linear Search to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Algorithms for the Main Data Structures

Stacks

user avatar
user avatar
user avatar
user avatar
user avatar

491+ studying

198KViews

96%

114 rated

Algorithms for the Main Data Structures

Queues

user avatar
user avatar
user avatar
user avatar
user avatar

270+ studying

194KViews

96%

114 rated

Algorithms for the Main Data Structures

Trees

user avatar
user avatar
user avatar
user avatar
user avatar

422+ studying

189KViews

96%

114 rated

Algorithms for the Main Data Structures

Linked List

user avatar
user avatar
user avatar
user avatar
user avatar

282+ studying

186KViews
Load more notes

Join 500,000+ A-Level students using SimpleStudy...

Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered