Linear Search (College Board AP® Computer Science Principles): Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

  • A linear search (also called a sequential search) checks each element in a list one at a time, from beginning to end, until the target value is found or the end of the list is reached

  • It is the simplest search algorithm and works on both sorted and unsorted lists

How a linear search works

  • A linear search uses a found flag, initialized to false, to track whether the target has been located

  • The loop checks each element in turn; if a match is found the flag is set to true

  • After the loop completes, the flag determines what the program displays

found ← false
FOR EACH item IN list
{
   IF(item = target)
   {
      found ← true
   }
}
IF(found = true)
{
   DISPLAY("Found")
}
ELSE
{
   DISPLAY("Not found")
}
 

Linear search characteristics

  • Best case: the target is the first element (1 comparison)

  • Worst case: the target is the last element or not in the list (n comparisons, where n is the length of the list)

  • The number of comparisons grows in proportion to the size of the list

  • Linear search does not require the list to be sorted

Characteristic

Detail

Example

Data requirement

Works on sorted or unsorted lists

Can search [8, 3, 15, 7, 12] without sorting first

Best case

1 comparison — target is the first element

Searching for 8 in [8, 3, 15, 7, 12] finds it immediately

Worst case

n comparisons — target is last or not in the list

Searching for 12 in [8, 3, 15, 7, 12] requires all 5 comparisons

Examiner Tips and Tricks

  • In code trace questions, track the found flag through each iteration and identify which element triggers it being set to true.

  • For the CPT, if your program searches a list, a linear search is the most straightforward approach to implement and explain in your written response.

Worked Example

A list contains the following values:

[8, 3, 15, 7, 12]

A linear search is performed to find the value 7. How many comparisons are made before the value is found?

(A) 2
(B) 3
(C) 4
(D) 5

[1]

Answer:

(C) 4 [1 mark]

  • The search checks elements in order: 8, 3, 15, then 7; the target is found on the fourth comparison

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.