Linear Search Implementation

Class LinearSearchTest (Fig. 19.2) contains static method linearSearch for performing searches of an int array and main for testing linearSearch.

 1   // Fig. 19.2: LinearSearchTest.java 2   // Sequentially searching an array for an item. 3   import java.security.SecureRandom; 4   import java.util.Arrays; 5   import java.util.Scanner; 6  7   public class LinearSearchTest 8   { 9      // perform a linear search on the data                   10      public static int linearSearch(int data[], int searchKey)11      {                                                        12         // loop through array sequentially                    13         for (int index = 0; index < data.length; ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.