Implemented InterpolationSearch.java
This commit is contained in:
parent
f842d5c2ea
commit
aec729b236
@ -1,144 +1,96 @@
|
|||||||
// Name: Jonathan Turner
|
|
||||||
// Class: CS 4306/01
|
|
||||||
// Term: Spring 2024
|
|
||||||
// Instructor: Dr. Haddad
|
|
||||||
// Assignment: 3
|
|
||||||
// IDE Name: IntelliJ
|
|
||||||
package Assignments.A3;
|
package Assignments.A3;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class is used to perform the interpolation search algorithm and display the results.
|
|
||||||
*
|
|
||||||
* @author Jonathan Turner
|
|
||||||
* @version Spring 2024
|
|
||||||
*/
|
|
||||||
public class InterpolationSearch {
|
public class InterpolationSearch {
|
||||||
|
|
||||||
/**
|
/** Holds if the value was found or not. */
|
||||||
* Starts the program.
|
private final boolean found;
|
||||||
* @param args the program arguments.
|
/** Holds the index of the key, -1 if not found. */
|
||||||
*/
|
private final int index;
|
||||||
public static void main(String[] args) {
|
/** Holds the number of divisions used to perform the search. */
|
||||||
InterpolationSearch program = new InterpolationSearch();
|
private int divisions;
|
||||||
program.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used as the main sentential loop and loops until the exit option is selected.
|
* Accepts the values, performs the search, and assigns the fields to the corresponding output data.
|
||||||
|
* <p>
|
||||||
|
* This constructor accepts in the array to be searched and the key to be searched for. Based off of this,
|
||||||
|
* the method will perform some precondition checks to ensure the values are valid and then perform the
|
||||||
|
* search using the formula provided in the slide deck.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @precondition none
|
* @param array The list of values being search through.
|
||||||
* @postcondition none
|
* @param key The desired value being searched for.
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException If the array is empty.
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public InterpolationSearch(int[] array, int key) {
|
||||||
int option = this.getOption();
|
/* Precondition for empty list. */
|
||||||
while (option != 4) {
|
if (array.length == 0) {
|
||||||
this.executeOption(option);
|
throw new IllegalArgumentException("The searched array cannot be empty.");
|
||||||
option = this.getOption();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs the action related to the option that was chosen.
|
|
||||||
*
|
|
||||||
* @precondition none
|
|
||||||
* @postcondition the action was performed.
|
|
||||||
*
|
|
||||||
* @param option the option that was chosen.
|
|
||||||
*/
|
|
||||||
private void executeOption(int option) {
|
|
||||||
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Uses private fields to generate the number of subsets and comparisons.
|
|
||||||
*
|
|
||||||
* @precondition text != null && text != empty
|
|
||||||
* @postcondition the results are generated.
|
|
||||||
*/
|
|
||||||
private void performAlgorithm() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints out the menu of options, asks for an input, and if that input is invalid it prints an error and
|
|
||||||
* prompts the user again for the input.
|
|
||||||
*
|
|
||||||
* @precondition none
|
|
||||||
* @postcondition none
|
|
||||||
*
|
|
||||||
* @return the option selected
|
|
||||||
*/
|
|
||||||
private int getOption() {
|
|
||||||
this.displayMenu(); // Prints out the menu
|
|
||||||
|
|
||||||
// Asks for the option and begins the loop until a valid option is gathered.
|
|
||||||
System.out.print("Enter option number: ");
|
|
||||||
int input = getIntegerInput();
|
|
||||||
while (input < 1 || input > 4) { // Compares it to the valid options available.
|
|
||||||
System.out.println("\nPlease enter a valid input.");
|
|
||||||
System.out.print("Enter option number: ");
|
|
||||||
input = getIntegerInput();
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays the menu options.
|
|
||||||
*
|
|
||||||
* @precondition none
|
|
||||||
* @postcondition the menu is displayed.
|
|
||||||
*/
|
|
||||||
private void displayMenu() {
|
|
||||||
System.out.println("-----------------MAIN MENU--------------");
|
|
||||||
System.out.println("1. Create, populate, and display array Values[]");
|
|
||||||
System.out.println("2. Read output table size");
|
|
||||||
System.out.println("3. Run interpolation search and display outputs");
|
|
||||||
System.out.println("4. Exit program");
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a string and returns it. If the input is not valid it loops until valid.
|
|
||||||
*
|
|
||||||
* @precondition none
|
|
||||||
* @postcondition none
|
|
||||||
* @return a user-input text string.
|
|
||||||
*/
|
|
||||||
private String getString() {
|
|
||||||
System.out.println("Specify a text to check number of substrings.");
|
|
||||||
System.out.print("Text: ");
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
String input = sc.nextLine();
|
|
||||||
|
|
||||||
while (input == null || input.isEmpty()) {
|
|
||||||
System.out.println("Please specify a valid text to check substrings.");
|
|
||||||
System.out.print("Text: ");
|
|
||||||
input = sc.nextLine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return input;
|
/* Precondition check to see if the list is size 1 element. */
|
||||||
|
if (array.length == 1) {
|
||||||
|
this.found = (array[0] == key);
|
||||||
|
this.divisions = 0;
|
||||||
|
this.index = (array[0] == key ? 0 : -1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sets the default values for the search */
|
||||||
|
int low = 0;
|
||||||
|
int high = array.length-1;
|
||||||
|
this.divisions = 0;
|
||||||
|
|
||||||
|
/* Iterates as long as high is not less than low */
|
||||||
|
while (high >= low) {
|
||||||
|
int probe;
|
||||||
|
|
||||||
|
/* Checks if high is the same thing as low to prevent division by zero. */
|
||||||
|
if (high != low) {
|
||||||
|
probe = low + (((key - array[low]) * (high - low)) / (array[high] - array[low]));
|
||||||
|
|
||||||
|
/* This is here due to the fact that it is not always divided as there is only 1 element left. */
|
||||||
|
this.divisions++;
|
||||||
|
} else {
|
||||||
|
probe = high;
|
||||||
|
}
|
||||||
|
/* Checks if the key is less than the probe's value */
|
||||||
|
if (array[probe] > key) {
|
||||||
|
high = probe - 1;
|
||||||
|
} else if (array[probe] < key) { /* Checks if the key is greater than the probes value */
|
||||||
|
low = probe + 1;
|
||||||
|
} else { /* The key was found at the probes index. */
|
||||||
|
this.found = true;
|
||||||
|
this.index = probe;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Sets the values if the key was not in the list. */
|
||||||
|
this.found = false;
|
||||||
|
this.index = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets user input to a number. A prompt must be provided prior to running this method.
|
* Gets if the key was found.
|
||||||
* If the input was not an integer/(unable to be autoboxed), returns -1.
|
* @return if it was found.
|
||||||
*
|
|
||||||
* @precondition none
|
|
||||||
* @postcondition none
|
|
||||||
*
|
|
||||||
* @return the integer input, if invalid -1.
|
|
||||||
*/
|
*/
|
||||||
private int getIntegerInput() {
|
public boolean isFound() {
|
||||||
Scanner sc = new Scanner(System.in);
|
return found;
|
||||||
try {
|
}
|
||||||
String textInput = sc.nextLine();
|
|
||||||
return Integer.parseInt(textInput);
|
/**
|
||||||
} catch (Exception e) {
|
* Gets the number of divisions it took for the search to conclude.
|
||||||
return -1;
|
* @return the number of divisions
|
||||||
}
|
*/
|
||||||
|
public int getDivisions() {
|
||||||
|
return divisions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the index at which the key was found. If the key was not found it returns -1.
|
||||||
|
* @return the found index.
|
||||||
|
*/
|
||||||
|
public int getIndex() {
|
||||||
|
return index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user