Java Array Linear Search

Description

Java Array Linear Search


public class Main {
   public static int linearSearch(int[] data, int target) {
      for (int i = 0; i < data.length; i++) {
         if (target == data[i])
            return i;
      }//from w  w w.java2s  .  co m
      return -1;
   }

   public static void main(String[] args) {
      int[] data = { 31, 14, 7, 22, 45, 12, 19, 42, 6 };
      System.out.println("Search for   7:" + linearSearch(data, 7));
      System.out.println("Search for 100:" + linearSearch(data, 100));
   }
}



PreviousNext

Related