From aec729b236278fd42bc6cd5c3fe9b2f131892a7c Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 21 Mar 2024 12:08:29 -0400 Subject: [PATCH] Implemented InterpolationSearch.java --- src/Assignments/A3/InterpolationSearch.java | 208 ++++++++------------ 1 file changed, 80 insertions(+), 128 deletions(-) diff --git a/src/Assignments/A3/InterpolationSearch.java b/src/Assignments/A3/InterpolationSearch.java index 1f813b2..43f8e8a 100644 --- a/src/Assignments/A3/InterpolationSearch.java +++ b/src/Assignments/A3/InterpolationSearch.java @@ -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; -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 { - /** - * Starts the program. - * @param args the program arguments. - */ - public static void main(String[] args) { - InterpolationSearch program = new InterpolationSearch(); - program.start(); - } + /** Holds if the value was found or not. */ + private final boolean found; + /** Holds the index of the key, -1 if not found. */ + private final int index; + /** Holds the number of divisions used to perform the search. */ + private int divisions; /** - * 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. + *

+ * 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. + *

* - * @precondition none - * @postcondition none + * @param array The list of values being search through. + * @param key The desired value being searched for. + * + * @throws IllegalArgumentException If the array is empty. */ - public void start() { - int option = this.getOption(); - while (option != 4) { - this.executeOption(option); - 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(); + public InterpolationSearch(int[] array, int key) { + /* Precondition for empty list. */ + if (array.length == 0) { + throw new IllegalArgumentException("The searched array cannot be empty."); } - 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. - * If the input was not an integer/(unable to be autoboxed), returns -1. - * - * @precondition none - * @postcondition none - * - * @return the integer input, if invalid -1. + * Gets if the key was found. + * @return if it was found. */ - private int getIntegerInput() { - Scanner sc = new Scanner(System.in); - try { - String textInput = sc.nextLine(); - return Integer.parseInt(textInput); - } catch (Exception e) { - return -1; - } + public boolean isFound() { + return found; + } + + /** + * Gets the number of divisions it took for the search to conclude. + * @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; } }