Updated LinearSearch.java

Converted integer linear search to a generic linear search and added faster input method
This commit is contained in:
Varun Upadhyay
2017-08-13 12:03:28 -07:00
committed by GitHub
parent a0aed9aef2
commit ee5974a04e

View File

@ -1,62 +1,77 @@
import java.util.Scanner; import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
* @author Varun Upadhyay (https://github.com/varunu28)
*
*/
public class LinearSearch{ public class LinearSearch{
/** /**
* The main method * The main method
* @param args Command line arguments * @param args Command line arguments
*/ */
public static void main(String[] args){ public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] myArray;
int size = 0;
//Prompt user to create array and its elements // Test for Integer inputs
System.out.print("Enter the array size: "); Integer[] myArray;
size = input.nextInt(); int size = 0;
myArray = new int[size];
for (int i = 0; i < size; i++){
System.out.print("For index " + i + ", enter an integer: ");
myArray[i] = input.nextInt();
}
//Prompt user to search for particular element //Prompt user to create array and its elements
System.out.print("Enter integer to search for: "); System.out.print("Enter the array size: ");
int key = input.nextInt(); size = Integer.parseInt(br.readLine());
myArray = new Integer[size];
for (int i = 0; i < size; i++){
System.out.print("For index " + i + ", enter an integer: ");
myArray[i] = Integer.parseInt(br.readLine());
}
//Output array and index of target element, if found //Prompt user to search for particular element
printarray(myArray); System.out.print("Enter integer to search for: ");
System.out.printf("The integer %d is found in index %d\n", key, linearsearch(myArray, key)); Integer key = Integer.parseInt(br.readLine());
input.close();
}
/** //Output array and index of target element, if found
* Linear search method System.out.printf("The integer %d is found in index %d\n", key, linearSearch(myArray, key));
*
* @param list List to be searched
* @param key Key being searched for
* @return Location of the key
*/
public static int linearsearch(int[] list, int key){
for (int i = 0; i< list.length; i++){ // Test for String inputs
if (list[i] == key){ String[] myArray1;
return i; int size1 = 0;
}
//Prompt user to create array and its elements
} return -1; System.out.print("Enter the array size: ");
} size1 = Integer.parseInt(br.readLine());
/** myArray1 = new String[size];
* Helper Method for (int i = 0; i < size1; i++){
* System.out.print("For index " + i + ", enter a String: ");
* @param a array to be printed myArray1[i] = br.readLine();
*/ }
public static void printarray(int[] a){
System.out.print("The array is: "); //Prompt user to search for particular element
for( int d: a){ System.out.print("Enter String to search for: ");
System.out.print(d+" "); String key1 = br.readLine();
}
System.out.println(); //Output array and index of target element, if found
} System.out.printf("The string %s is found in index %d\n", key1, linearSearch(myArray1, key1));
}
/**
* Generic Linear search method
*
* @param array List to be searched
* @param value Key being searched for
* @return Location of the key
*/
public static <T extends Comparable<T>> int linearSearch(T[] array, T value) {
int lo = 0;
int hi = array.length - 1;
for (int i = lo; i <= hi; i++) {
if (array[i].compareTo(value) == 0) {
return i;
}
}
return -1;
}
} }