00:00

Linear Search Algorithm in Java

Last updated:

A search algorithm answers the question "is this value in my collection, and if so, where?". The right algorithm depends on how the data is organised: an unsorted array, a sorted array, a tree or a hash table each allow different strategies.

This tutorial covers the simplest one, linear search, which works on any sequence, sorted or not.

What is a Linear Search Algorithm?

Linear search (also called sequential search) checks the elements one after another, from the first to the last, until it finds one equal to the target. If it reaches the end without a match, the target is not in the collection.

It needs no preparation and makes no assumption about the order of the data, so it works on unsorted arrays, linked lists and any other structure that can be walked element by element.

How Does the Linear Search Algorithm Work?

  1. Start at the first element (index 0).

  2. Compare the current element with the target.

  3. If they are equal, stop and return the current index.

  4. Otherwise move to the next element and repeat from step 2.

  5. If the end of the array is reached without a match, return −1 to signal that the target is not present.

Implementation of Linear Search Algorithm in Java

The method returns the 0-based index of the first match, or −1. The program also prints the 1-based position for readers who count from 1.

LinearSearchExample.java
// Linear (sequential) search: check the elements one by one
public class LinearSearchExample {

  // Returns the index of the first element equal to target, or -1 if there is none
  static int linearSearch(int[] array, int target) {
    for (int i = 0; i < array.length; i++) {
      if (array[i] == target) {
        return i;            // stop at the first match
      }
    }
    return -1;               // checked every element: not present
  }

  static void report(int[] array, int target) {
    int index = linearSearch(array, target);
    if (index >= 0) {
      System.out.println(target + " is found at index " + index + " (position " + (index + 1) + ")");
    } else {
      System.out.println(target + " is not present in the array");
    }
  }

  public static void main(String[] args) {
    int[] array = {25, 80, 75, 35, 44, 90, 62};
    report(array, 44);       // present
    report(array, 54);       // not present
  }
}
Output
44 is found at index 4 (position 5)
54 is not present in the array

Time and Space Complexity of Linear Search Algorithm

  • Time: best case O(1) when the target is the first element; worst case O(n) when it is the last element or absent (n comparisons); average case O(n), about n/2 comparisons when the target is present at a random position.

  • Space: O(1) extra; only a loop index is used.

Applications of Linear Search Algorithm

  • Unsorted data: when the data is not sorted and is searched only once or rarely, sorting first would cost more than a single linear scan.

  • Small collections: for a handful of elements a linear scan is as fast as anything else and simpler.

  • Linked lists: without random access, a linked list can only be searched sequentially.

  • Finding every match or a match by a condition: for example all customers in a given city, or the first value above a threshold.

Advantages of Linear Search Algorithm

  • Very simple to write and to get right.

  • Works on sorted and unsorted data, and on any structure with sequential access.

  • Needs no extra memory and no preprocessing such as sorting or building an index.

  • O(1) when the target is near the front.

Disadvantages of Linear Search Algorithm

  • O(n) in the worst and average case, which is slow for large collections.

  • Wasteful when the same data is searched many times: sorting once and using binary search (O(log n)), or using a HashSet/HashMap (O(1) on average), is much faster.

  • Does not take advantage of sorted data.