A linear search algorithm checks each element of an array or list one at a time, in order, until it finds the target value or reaches the end. In AP CSA, it's written with a loop that compares each element to the target and typically returns the index if found, or -1 (or false) if not.
A linear search (also called sequential search) is the most straightforward way to find a value in a collection. You start at the first element, compare it to the target value, and if it doesn't match, you move to the next one. You keep going until you either find a match or run out of elements. That's the whole algorithm.
In AP CSA, you write linear search as a for loop (or while loop) over an array or ArrayList. The standard pattern returns the index of the first match, and returns a sentinel value like -1 if the loop finishes without finding anything. That last part is the detail that trips people up. The only way a linear search knows a value is not present is by checking every single element and reaching the end without a match. Unlike binary search, linear search works on unsorted data, which is exactly why it's the default tool when you can't assume anything about the order of the elements.
Linear search is one of the named algorithms the AP CSA course expects you to write, trace, and analyze. It shows up wherever array and ArrayList traversal is taught, and it's the foundation for the course's discussion of algorithm efficiency. When the exam asks you to compare how many comparisons two searches make, linear search is the baseline (worst case, it checks all n elements). It also reinforces core skills the whole course is built on, like loop bounds, index-based access, early returns, and the off-by-one errors that come with all three. If you can write a clean linear search from scratch, you've proven you understand iteration, indexing, and return values all at once.
Iteration (Loops & Traversal)
Linear search is iteration with a job. It's the same loop you use to traverse an array, plus one if statement comparing each element to the target. If you can write a standard for loop over an array, you're one comparison away from a linear search.
Index (Arrays & ArrayLists)
Linear search usually returns the index of the match, not the value itself. You already know the value (it's the target). What you're hunting for is where it lives, which is why the loop variable doubles as the answer.
Return Value (Methods)
The classic linear search method has two exits. It returns the index immediately when it finds a match (an early return inside the loop), and returns -1 after the loop if nothing matched. Putting return -1 inside the loop instead of after it is one of the most common bugs on FRQ-style code.
Target Value (Searching)
The target value is what you're comparing against on every iteration. Watch the data type here. For int arrays you compare with ==, but for String or object elements you must use .equals(), a distinction MCQs love to test.
Linear search shows up in both multiple-choice and free-response contexts. MCQs ask you to identify its defining behavior (checking elements sequentially, one by one), trace how many comparisons it makes for a given input, or recognize how it concludes a value is absent (it must reach the end of the list without a match). Practice questions hit exactly these angles: what the primary characteristic of linear search is, and how it determines a value isn't present. On FRQs, you're often asked to write a search as part of a larger method, like finding the index of a value in an array or checking whether an ArrayList contains an element. Know the pattern cold: loop over every element, compare to the target, return the index on a match, return -1 (or false) after the loop. Also be ready to compare it to binary search, since efficiency comparisons between the two are a classic question type.
Linear search checks every element in order and works on any list, sorted or not. Binary search repeatedly cuts a sorted list in half, which makes it much faster (about log₂(n) comparisons instead of up to n) but useless on unsorted data. If an exam question says the array is unsorted, binary search is off the table and linear search is your answer. If it says sorted and asks for the fewest comparisons, think binary.
Linear search checks each element one at a time, in order, until it finds the target value or reaches the end of the array or list.
It concludes a value is NOT present only after checking every element without finding a match, which is its worst case at n comparisons.
The standard AP CSA pattern returns the index of the first match inside the loop and returns -1 after the loop if no match exists.
Linear search works on unsorted data, while binary search requires sorted data, and that's the main tradeoff the exam asks you to recognize.
When searching for Strings or objects, compare with .equals() instead of ==, since == checks references, not contents.
It's a searching algorithm that checks each element of an array or ArrayList one by one, in order, until it finds the target value or reaches the end. In AP CSA you implement it with a loop that returns the matching index, or -1 if the value isn't found.
Only by checking every single element and reaching the end without a match. That's why the return -1 belongs after the loop, not inside it. Returning -1 on the first non-match is a classic bug that makes the method give up after one comparison.
Linear search checks elements sequentially and works on unsorted data, taking up to n comparisons. Binary search halves a sorted list each step, taking about log₂(n) comparisons, but it fails on unsorted data. For a 1,000-element sorted array, that's up to 1,000 comparisons versus about 10.
No. That's linear search's biggest advantage. It works on completely unsorted data because it checks every element regardless of order. Sorting is only required for binary search.
Yes. It's one of the standard algorithms in the course, and you should be able to write it, trace it, count its comparisons, and explain when it's required instead of binary search (any time the data is unsorted). Search logic also appears embedded inside larger FRQ methods.