Provide Best Programming Tutorials

Java Linear Search Approch

The linear search approach compares the key element key sequentially with each element in the array. It continues to do so until the key matches an element in the array, or the array is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the array that matches the key. If no match is found, the search returns −1 .

The linear search  method gives the solution.

public class LinearSearch {
  /** The method for finding a key in the list */
  public static int linearSearch(int[] list, int key) {
    for (int i = 0; i < list.length; i++) {
      if (key == list[i])
        return i;
    return -1;


The process is described as below picture shown

Check out this link to see the animation of the linear search process:

Leave a Reply

Close Menu