-
-
Save bytecodeman/15fe34b5ecfbaf0167281e84d65a5d1d to your computer and use it in GitHub Desktop.
The Ultimate Linear Search Class V3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package silvestri; | |
public class LinearSearch { | |
public enum SearchMode { | |
CASESENSITIVE, CASEINSENSITIVE | |
}; | |
// start is the very first index that is include in search | |
// end is the index value that is not included in search | |
// This is the technique used in Java and Python | |
private static void checkStartEndIndices(int listLength, int start, int end) { | |
String descriptor = "Bad %1$s Index!!! %1$s = %d list.length = %d"; | |
if (start < 0 || start >= listLength) { | |
throw new RuntimeException(String.format(descriptor, "Start", start, listLength)); | |
} | |
if (end < 0 || end > listLength) { | |
throw new RuntimeException(String.format(descriptor, "End", end, listLength)); | |
} | |
} | |
public static int search(char[] list, int start, int end, char key) { | |
checkStartEndIndices(list.length, start, end); | |
for (int i = start; i < end; i++) { | |
if (key == list[i]) | |
return i; | |
} | |
return -1; | |
} | |
public static int search(int[] list, int start, int end, int key) { | |
checkStartEndIndices(list.length, start, end); | |
for (int i = start; i < end; i++) { | |
if (key == list[i]) | |
return i; | |
} | |
return -1; | |
} | |
public static int search(double[] list, int start, int end, double key) { | |
checkStartEndIndices(list.length, start, end); | |
for (int i = start; i < end; i++) { | |
if (key == list[i]) | |
return i; | |
} | |
return -1; | |
} | |
public static int search(String[] list, int start, int end, String key, SearchMode mode) { | |
checkStartEndIndices(list.length, start, end); | |
if (mode == SearchMode.CASESENSITIVE) { | |
for (int i = start; i < end; i++) { | |
if (key.equals(list[i])) | |
return i; | |
} | |
} | |
else { | |
for (int i = start; i < end; i++) { | |
if (key.equalsIgnoreCase(list[i])) | |
return i; | |
} | |
} | |
return -1; | |
} | |
public static int search(String[] list, String key, SearchMode mode) { | |
return search(list, 0, list.length, key, mode); | |
} | |
public static int search(char[] list, char key) { | |
return search(list, 0, list.length, key); | |
} | |
public static int search(int[] list, int key) { | |
return search(list, 0, list.length, key); | |
} | |
public static int search(double[] list, double key) { | |
return search(list, 0, list.length, key); | |
} | |
public static int search(String[] list, String key) { | |
return search(list, 0, list.length, key, SearchMode.CASESENSITIVE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: | |
* Date: | |
* Course Number: | |
* Course Name: | |
* Problem Number: | |
* Email: | |
* Short Description of the Problem | |
*/ | |
import java.util.Arrays; | |
import java.util.Scanner; | |
import silvestri.*; | |
import silvestri.LinearSearch.SearchMode; | |
public class TestLinearSearch { | |
private final static String TITLE = "Testing Linear Search Algorithm V1.0"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
//********************************************** | |
// Put as many methods you need here | |
//********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner sc, String args[]) { | |
int iArr[] = {2, 31, 22, 61, 5, 68, 45, 58, 70, 82}; | |
String sArr[] = { "John", "Paul", "George", "Ringo", "Steve", | |
"Mark", "Tony", "Mary", "Bill", "Tim" }; | |
System.out.println("Testing with integer array"); | |
System.out.println(Arrays.toString(iArr)); | |
do { | |
System.out.print("Enter key: "); | |
int key = sc.nextInt(); | |
sc.nextLine(); | |
if (key < 0) | |
break; | |
int location = LinearSearch.search(iArr, key); | |
System.out.println("Key Found at Location " + location); | |
} while (true); | |
System.out.println("Testing with String array"); | |
System.out.println(Arrays.toString(sArr)); | |
do { | |
System.out.print("Enter key: "); | |
String key = sc.nextLine(); | |
if (key.equalsIgnoreCase("EXIT")) | |
break; | |
int location = LinearSearch.search(sArr, key, SearchMode.CASEINSENSITIVE); | |
System.out.println("Key Found at Location " + location); | |
} while (true); | |
//sc.nextLine(); // Clear Keyboard | |
} | |
//********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
//********************************************** | |
// Do not change the main method | |
public static void main(String args[]) { | |
System.out.println("Welcome to " + TITLE); | |
Scanner sc = new Scanner(System.in); | |
do { | |
process(sc, args); | |
} while (doThisAgain(sc, CONTINUE_PROMPT)); | |
sc.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment