Skip Top Navigation Bar

Linear Search Algorithm

Searching is the process of finding a specific value in a collection of data, such as an array. Two common search algorithms are linear search and binary search.

Both algorithms look for a target value, but they do so in different ways. Choosing the right algorithm depends on how the data is organized.

A linear search checks each element one at a time, starting at the beginning of the array.

If the current element matches the target value, the search stops. Otherwise, the algorithm continues to the next element until it finds the target or reaches the end of the array.

For example, suppose you want to find the value 18.

A linear search checks the values in this order:

  1. Check 12.
  2. Check 7.
  3. Check 18. The value is found.

Because linear search examines one element at a time, it works with any array, whether the values are sorted or not.

Linear search is a good choice when:

Your Turn

Let's try it in the Java Playground.