LinearSearcher.java.html

  1  /**
  2     A class for executing linear searches in an array.
  3  */
  4  public class LinearSearcher
  5  {  
  6     /**
  7        Finds a value in an array, using the linear search 
  8        algorithm.
  9        @param a the array to search
 10        @param value the value to find
 11        @return the index at which the value occurs, or -1
 12        if it does not occur in the array
 13     */
 14     public static int search(int[] a, int value)
 15     {  
 16        for (int i = 0; i < a.length; i++)
 17        {  
 18           if (a[i] == value) { return i; }
 19        }
 20        return -1;
 21     }
 22  }