Linear Search

Linear search (also called sequential search) is an algorithm that checks each element in a list one at a time, in order, until it finds the target value or reaches the end. In AP CSP, it's the search that works on any list, sorted or not, but is usually slower than binary search on sorted data.

Verified for the 2027 AP Computer Science Principles examLast updated June 2026

What is Linear Search?

A linear search starts at the first element of a list and compares each element to the value you're looking for, one by one, until it either finds a match or runs out of elements. The College Board also calls this sequential search, and the two names mean exactly the same thing. If you see either one on the exam, picture the same process of walking down the list in order.

In AP pseudocode, a linear search is just a list traversal with a comparison inside. You can write it with FOR EACH item IN aList from the exam reference sheet, checking whether item equals your target. That makes linear search the most natural example of a traversal that can stop early. If the target is the first element, you do a partial traversal and you're done in one check. If the target isn't in the list at all, you do a complete traversal and check every single element. That worst case is exactly why the CED says binary search is often more efficient on sorted data (EK AAP-2.P.3).

Why Linear Search matters in AP Computer Science Principles

Linear search lives in Unit 3: Algorithms and Programming, mostly inside Topic 3.11 (Binary Search) and Topic 3.10 (Lists). Learning objective AP Comp Sci P 3.11.A asks you to explain the requirements for binary search, and linear search is the contrast that makes those requirements make sense. Binary search demands sorted data (EK AAP-2.P.2); linear search demands nothing. It works on any list. The trade-off is speed. On a sorted list of 1000 items, binary search needs at most about 10 checks, while linear search might need all 1000.

Linear search also gives you a concrete payoff for the traversal skills in AP Comp Sci P 3.10.B. When you write a FOR EACH loop that hunts for a value, you're literally writing a linear search. So this one term ties together list indexing, traversal, and algorithm efficiency, three of the biggest ideas in Unit 3.

How Linear Search connects across the course

Binary Search (Unit 3)

Binary search is linear search's faster rival. It jumps to the middle of a sorted list and throws away half the data each step (EK AAP-2.P.1). Linear search exists in the CED mostly so you can explain when binary search wins (sorted data) and when it can't even be used (unsorted data).

Sequential Search (Unit 3)

This is the same algorithm with a different name. The CED itself says "sequential/linear search" in one breath, so treat the two terms as interchangeable on exam day.

Complete Traversal (Unit 3)

A linear search's worst case is a complete traversal. If the target isn't in the list, you visit every element before you can say "not found." If you get lucky and the target sits near the front, it's only a partial traversal.

Algorithm (Unit 3)

Linear search is one of the cleanest examples of an algorithm: a finite, step-by-step process with a clear stopping condition. It's also Exhibit A for comparing algorithm efficiency, since two algorithms (linear and binary) solve the same problem at very different speeds.

Is Linear Search on the AP Computer Science Principles exam?

Linear search shows up almost entirely in multiple-choice questions, and almost always as the foil to binary search. Common stems ask for the primary advantage of binary search over linear search (fewer comparisons on sorted data), a limitation of binary search compared to linear search (binary requires sorted data, linear doesn't), or which method is more efficient for a large sorted dataset (binary). You may also get a scenario question, like a programmer with an unsorted list of 1000 student IDs who needs frequent lookups, where you have to weigh sorting once then binary searching versus linear searching every time. You won't be asked to write a binary search implementation (the CED excludes that), but you should be able to read a FOR EACH loop and recognize it as a linear search, then trace what it returns.

Linear Search vs Binary Search

Linear search checks elements one at a time from the front and works on any list. Binary search starts at the middle of a sorted list and eliminates half the remaining data with each comparison. The key trade-off the exam loves: binary search is usually faster, but it only works on sorted data, while linear search works everywhere. If a question says the list is unsorted, binary search is off the table unless you sort first.

Key things to remember about Linear Search

  • Linear search and sequential search are two names for the same algorithm, which checks each list element in order until it finds the target or hits the end.

  • Linear search works on any list, sorted or unsorted, which is its one advantage over binary search.

  • In the worst case, linear search checks every element (a complete traversal of n items), while binary search on sorted data eliminates half the remaining elements per step.

  • Binary search requires sorted data (EK AAP-2.P.2), so an unsorted list forces you to either use linear search or sort the data first.

  • In AP pseudocode, a linear search is just a FOR EACH loop over a list with an equality check inside, so traversal skills from Topic 3.10 are all you need to write one.

Frequently asked questions about Linear Search

What is linear search in AP Computer Science Principles?

Linear search is an algorithm that checks each element of a list one at a time, in order, until it finds the target value or reaches the end. The College Board also calls it sequential search, and it appears in Unit 3 alongside binary search.

Is linear search the same as sequential search?

Yes, they're identical. The CED uses "sequential/linear search" as one term, so any exam question using either name is asking about the same check-every-element algorithm.

Is linear search always slower than binary search?

No. Binary search is usually faster on sorted data, but it can't run on unsorted data at all. Linear search can also win small cases, like when the target happens to be the first element, where it finishes in a single check.

How is linear search different from binary search?

Linear search starts at the front and checks every element in order, while binary search starts at the middle of a sorted list and eliminates half the data each step. For 1000 sorted items, binary search needs at most about 10 comparisons; linear search could need all 1000.

Do I need to write linear search code on the AP CSP exam?

You won't implement binary search (the CED excludes specific implementations), but you should be able to read and trace a linear search written as a FOR EACH loop in AP pseudocode and predict what it outputs. Multiple-choice questions also test when to choose linear versus binary search.