Search⌘ K
AI Features

Linear Search

Explore the linear search algorithm to understand how it sequentially examines each element in a collection until finding the target value or reaching the end. Learn its practical implementation, use cases, and analyze its time and space complexity to grasp when and how to apply this foundational searching technique effectively.

Linear search is the simplest searching algorithm. It works by checking each element in turn until the target value is found or the collection ends.

Although it is simple, linear search is an important foundational concept. It helps learners understand how a search moves through data before we study faster algorithms, such as binary search.

Real-world intuition

Imagine you are a postal worker on Maple Street. Your job is to deliver a parcel to house number 47. The houses are not in any particular order. They were built at different times, and the numbers were assigned as construction was completed.

Because the houses are unordered, you have no shortcut. You start at the first house, check its number, and move on if it is not the one you need. You continue this process, one house at a time, until you either find number 4747 or reach the end of the street without finding it.

Linear search is an algorithm that checks each element in a collection one by one until the target is found or the collection is exhausted.

This is the most straightforward searching strategy. It requires no prior knowledge about the data and no sorting or preprocessing. This simplicity is both its greatest strength and its main limitation.

How it works

Linear search follows a simple step-by-step process. At each step, it compares the current element with the target and stops when one of the following conditions is met:

  • If the target is found, the algorithm stops immediately.

  • If the end of the collection is reached without a match, the search fails.

Key property: The search traverses the data in a linear sequence. It never jumps ahead, backtracks, or skips elements. Every element between the start and the target will be examined exactly once. ...

Step-by-step trace