Compare commits

..

No commits in common. "b97fb17561ae736d2ff5846aefb7c98600268459" and "f842d5c2eabc2bbeeee8a6d0eef6065a4a58dc2b" have entirely different histories.

2 changed files with 120 additions and 123 deletions

View File

@ -1,96 +1,144 @@
// 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. */ /**
private final boolean found; * Starts the program.
/** Holds the index of the key, -1 if not found. */ * @param args the program arguments.
private final int index; */
/** Holds the number of divisions used to perform the search. */ public static void main(String[] args) {
private int divisions; InterpolationSearch program = new InterpolationSearch();
program.start();
}
/** /**
* Accepts the values, performs the search, and assigns the fields to the corresponding output data. * Used as the main sentential loop and loops until the exit option is selected.
* <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>
* *
* @param array The list of values being search through. * @precondition none
* @param key The desired value being searched for. * @postcondition none
*/
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.
* *
* @throws IllegalArgumentException If the array is empty. * @precondition none
* @postcondition the action was performed.
*
* @param option the option that was chosen.
*/ */
public InterpolationSearch(int[] array, int key) { private void executeOption(int option) {
/* Precondition for empty list. */
if (array.length == 0) {
throw new IllegalArgumentException("The searched array cannot be empty.");
}
/* Precondition check to see if the list is size 1 element. */ System.out.println();
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 if the key was found. * Uses private fields to generate the number of subsets and comparisons.
* @return if it was found. *
* @precondition text != null && text != empty
* @postcondition the results are generated.
*/ */
public boolean isFound() { private void performAlgorithm() {
return found;
} }
/** /**
* Gets the number of divisions it took for the search to conclude. * Prints out the menu of options, asks for an input, and if that input is invalid it prints an error and
* @return the number of divisions * prompts the user again for the input.
*
* @precondition none
* @postcondition none
*
* @return the option selected
*/ */
public int getDivisions() { private int getOption() {
return divisions; 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;
} }
/** /**
* Gets the index at which the key was found. If the key was not found it returns -1. * Displays the menu options.
* @return the found index. *
* @precondition none
* @postcondition the menu is displayed.
*/ */
public int getIndex() { private void displayMenu() {
return index; 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;
}
/**
* 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.
*/
private int getIntegerInput() {
Scanner sc = new Scanner(System.in);
try {
String textInput = sc.nextLine();
return Integer.parseInt(textInput);
} catch (Exception e) {
return -1;
}
} }
} }

View File

@ -1,51 +0,0 @@
package Assignments.A3;
import java.util.*;
public class TestInterpolationSearch {
/** Holds the list of 1024 unique values used to search through */
public int[] values;
/** Holds the size of the table to generate */
public int tableSize;
/**
* Generates a list of 1024 random, non-repeating, integers and assigns them to the values field.
*/
public void randomDistinct() {
/* Used to track what ints have been used */
HashSet<Integer> isUsed = new HashSet<>();
int[] newList = new int[1024];
Random random = new Random();
/* Loops 1024 times and assigns a new unique value each time. */
for (int i = 0; i < 1024; i++) {
int newValue = -1;
/* Ensures that there is no duplicate values */
do {
newValue = random.nextInt(9999) + 1;
} while (isUsed.contains(newValue));
/* Adds the value to the known list and also to the generated list. */
newList[i] = (newValue);
isUsed.add(newValue);
}
/* Sorts and assigns the values generated to the field. */
Arrays.sort(newList);
this.values = newList;
}
public void runIntSearch() {
int totalDivisions;
Random rand = new Random();
System.out.println(" Key\t\tFound\t\tIndex\t\tDivisions");
System.out.println("----------------------------------------------");
for (int i = 0; i < this.tableSize; i++) {
int key = rand.nextInt(9999) + 1;
InterpolationSearch results = new InterpolationSearch(this.values, key);
System.out.println(" " + key + "\t\t" + results.isFound() + "\t\t" + results.getIndex() + "\t\t" + results.getDivisions());
}
}
}