|
1 | | -import java.util.Scanner; |
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.InputStreamReader; |
| 3 | + |
| 4 | +/** |
| 5 | + * |
| 6 | + * @author Varun Upadhyay (https://github.com/varunu28) |
| 7 | + * |
| 8 | + */ |
2 | 9 |
|
3 | 10 | public class LinearSearch{ |
4 | | - /** |
5 | | - * The main method |
6 | | - * @param args Command line arguments |
7 | | - */ |
8 | | - public static void main(String[] args){ |
9 | | - |
10 | | - Scanner input = new Scanner(System.in); |
11 | | - int[] myArray; |
12 | | - int size = 0; |
13 | | - |
14 | | - //Prompt user to create array and its elements |
15 | | - System.out.print("Enter the array size: "); |
16 | | - size = input.nextInt(); |
17 | | - myArray = new int[size]; |
18 | | - for (int i = 0; i < size; i++){ |
19 | | - System.out.print("For index " + i + ", enter an integer: "); |
20 | | - myArray[i] = input.nextInt(); |
21 | | - } |
22 | | - |
23 | | - //Prompt user to search for particular element |
24 | | - System.out.print("Enter integer to search for: "); |
25 | | - int key = input.nextInt(); |
26 | | - |
27 | | - //Output array and index of target element, if found |
28 | | - printarray(myArray); |
29 | | - System.out.printf("The integer %d is found in index %d\n", key, linearsearch(myArray, key)); |
30 | | - |
31 | | - input.close(); |
32 | | - } |
33 | | - |
34 | | - /** |
35 | | - * Linear search method |
36 | | - * |
37 | | - * @param list List to be searched |
38 | | - * @param key Key being searched for |
39 | | - * @return Location of the key |
40 | | - */ |
41 | | - public static int linearsearch(int[] list, int key){ |
42 | | - |
43 | | - for (int i = 0; i< list.length; i++){ |
44 | | - if (list[i] == key){ |
45 | | - return i; |
46 | | - } |
47 | | - |
48 | | - } return -1; |
49 | | - } |
50 | | - /** |
51 | | - * Helper Method |
52 | | - * |
53 | | - * @param a array to be printed |
54 | | - */ |
55 | | - public static void printarray(int[] a){ |
56 | | - System.out.print("The array is: "); |
57 | | - for( int d: a){ |
58 | | - System.out.print(d+" "); |
59 | | - } |
60 | | - System.out.println(); |
61 | | - } |
| 11 | + /** |
| 12 | + * The main method |
| 13 | + * @param args Command line arguments |
| 14 | + */ |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + |
| 17 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 18 | + |
| 19 | + // Test for Integer inputs |
| 20 | + Integer[] myArray; |
| 21 | + int size = 0; |
| 22 | + |
| 23 | + //Prompt user to create array and its elements |
| 24 | + System.out.print("Enter the array size: "); |
| 25 | + size = Integer.parseInt(br.readLine()); |
| 26 | + myArray = new Integer[size]; |
| 27 | + for (int i = 0; i < size; i++){ |
| 28 | + System.out.print("For index " + i + ", enter an integer: "); |
| 29 | + myArray[i] = Integer.parseInt(br.readLine()); |
| 30 | + } |
| 31 | + |
| 32 | + //Prompt user to search for particular element |
| 33 | + System.out.print("Enter integer to search for: "); |
| 34 | + Integer key = Integer.parseInt(br.readLine()); |
| 35 | + |
| 36 | + //Output array and index of target element, if found |
| 37 | + System.out.printf("The integer %d is found in index %d\n", key, linearSearch(myArray, key)); |
| 38 | + |
| 39 | + // Test for String inputs |
| 40 | + String[] myArray1; |
| 41 | + int size1 = 0; |
| 42 | + |
| 43 | + //Prompt user to create array and its elements |
| 44 | + System.out.print("Enter the array size: "); |
| 45 | + size1 = Integer.parseInt(br.readLine()); |
| 46 | + myArray1 = new String[size]; |
| 47 | + for (int i = 0; i < size1; i++){ |
| 48 | + System.out.print("For index " + i + ", enter a String: "); |
| 49 | + myArray1[i] = br.readLine(); |
| 50 | + } |
| 51 | + |
| 52 | + //Prompt user to search for particular element |
| 53 | + System.out.print("Enter String to search for: "); |
| 54 | + String key1 = br.readLine(); |
| 55 | + |
| 56 | + //Output array and index of target element, if found |
| 57 | + System.out.printf("The string %s is found in index %d\n", key1, linearSearch(myArray1, key1)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Generic Linear search method |
| 62 | + * |
| 63 | + * @param array List to be searched |
| 64 | + * @param value Key being searched for |
| 65 | + * @return Location of the key |
| 66 | + */ |
| 67 | + public static <T extends Comparable<T>> int linearSearch(T[] array, T value) { |
| 68 | + int lo = 0; |
| 69 | + int hi = array.length - 1; |
| 70 | + for (int i = lo; i <= hi; i++) { |
| 71 | + if (array[i].compareTo(value) == 0) { |
| 72 | + return i; |
| 73 | + } |
| 74 | + } |
| 75 | + return -1; |
| 76 | + } |
62 | 77 | } |
0 commit comments