A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Linear Search

College Depth 93 in the knowledge graph ☐ I know this ☆ Set as goal
48topics build on this
409prerequisites beneath it
See this on the map →
Arrays and ListsComparison Operators and Boolean Tests+2 more→→Binary Search
searching linear-search sequential-search algorithms arrays AP-CSA

Core Idea

Linear search (also called sequential search) finds a target value in a list by checking the elements one at a time, in order, until it finds a match or reaches the end. It returns the position of the first match, or a signal such as -1 when the target is absent. It works on any list, sorted or not. For a list of n elements it makes at most n comparisons, so its worst-case running time is O(n).

How It's Best Learned

Act it out with a row of face-down cards: turn them over one by one and count the flips needed for different targets, including one that is missing. Then write the loop and trace it on a target at the front, in the middle, at the end, and absent. Count comparisons for each case before introducing big-O, and finally compare the counts with binary search on a sorted list of the same size.

Common Misconceptions

Explainer

Searching is one of the most common jobs a program does: find a name in a contact list, a product in an inventory, a word in a document. The simplest method is the one you would use with a messy stack of papers. Look at the first one. If it is not what you want, look at the next. Keep going until you find it or run out.

In code, linear search is a single loop:

```python

def linear_search(items, target):

for i in range(len(items)):

if items[i] == target:

return i

return -1

```

Notice where `return -1` sits. It is after the loop, not inside it. The function can say "not found" only after every element has been checked. Putting the "not found" return in an `else` branch inside the loop is one of the most common beginner bugs, because it makes the function give up after the first element.

How much work does it do? Count comparisons. If the target is first, one comparison. If it is last, or not present, every element is compared, so n comparisons for a list of n items. If the target is equally likely to be anywhere in the list, the average is about (n + 1) / 2. Computer scientists usually describe the worst case, so linear search is O(n): doubling the list roughly doubles the worst-case time.

Linear search has real strengths. It works on unsorted data. It works on structures you can only walk through in order, such as linked lists or a file being read line by line. It is short and hard to get wrong once the return is placed correctly. It also adapts easily: you can search for the largest value, for the first item meeting a condition, or for every match by collecting indexes instead of returning early.

Its weakness appears with large data searched often. A million-element list can require a million comparisons per search. If the list is sorted, binary search can find any item in about 20 comparisons by repeatedly halving the range. Sorting has its own cost, so the right choice depends on how often you search. For one search of unsorted data, linear search is the better choice. For many searches of the same data, sorting once and then using binary search is usually worth it.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Asymptotic Notation: Big-O, Big-Omega, Big-Theta → Big-O Notation and Complexity Analysis → Linear Search

Longest path: 94 steps · 409 total prerequisite topics

Prerequisites (4)

Leads To (1)