Advertisement
hendrtd

LinearSearch.java

Feb 8th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. /**
  2.  * LinearSearch.java
  3.  * Provides various implementations of linear search as relevant
  4.  * to class. All methods are written for arrays of ints.
  5.  *
  6.  * @author  Dean Hendrix (dh@auburn.edu)
  7.  * @version 2016-02-08
  8.  *
  9.  */
  10. public class LinearSearch {
  11.  
  12.     /**
  13.      * Uses linear search to search for value in a. Returns the
  14.      * location of value in a or -1 if a does not contain value.
  15.      *
  16.      * @param  a     the array to be searched
  17.      * @param  value the value to search for
  18.      * @return       index of value in a or -1 if not present
  19.      */
  20.     public static int lsearch1(int[] a, int value) {
  21.         int i = 0;
  22.         while ((i < a.length) && (a[i] != value)) {
  23.             i++;
  24.         }
  25.         if (i < a.length) {
  26.             return i;
  27.         }
  28.         else {
  29.             return -1;
  30.         }
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement