Compare commits
3 Commits
f842d5c2ea
...
b97fb17561
Author | SHA1 | Date | |
---|---|---|---|
|
b97fb17561 | ||
|
d31dbbb4d8 | ||
|
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;
|
||||
|
||||
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 {
|
||||
|
||||
/** 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;
|
||||
|
||||
/**
|
||||
* Starts the program.
|
||||
* @param args the program arguments.
|
||||
* 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>
|
||||
*
|
||||
* @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 static void main(String[] args) {
|
||||
InterpolationSearch program = new InterpolationSearch();
|
||||
program.start();
|
||||
public InterpolationSearch(int[] array, int key) {
|
||||
/* 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. */
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used as the main sentential loop and loops until the exit option is selected.
|
||||
*
|
||||
* @precondition none
|
||||
* @postcondition none
|
||||
* Gets if the key was found.
|
||||
* @return if it was found.
|
||||
*/
|
||||
public void start() {
|
||||
int option = this.getOption();
|
||||
while (option != 4) {
|
||||
this.executeOption(option);
|
||||
option = this.getOption();
|
||||
}
|
||||
public boolean isFound() {
|
||||
return found;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the action related to the option that was chosen.
|
||||
*
|
||||
* @precondition none
|
||||
* @postcondition the action was performed.
|
||||
*
|
||||
* @param option the option that was chosen.
|
||||
* Gets the number of divisions it took for the search to conclude.
|
||||
* @return the number of divisions
|
||||
*/
|
||||
private void executeOption(int option) {
|
||||
|
||||
System.out.println();
|
||||
public int getDivisions() {
|
||||
return divisions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses private fields to generate the number of subsets and comparisons.
|
||||
*
|
||||
* @precondition text != null && text != empty
|
||||
* @postcondition the results are generated.
|
||||
* Gets the index at which the key was found. If the key was not found it returns -1.
|
||||
* @return the found index.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
51
src/Assignments/A3/TestInterpolationSearch.java
Normal file
51
src/Assignments/A3/TestInterpolationSearch.java
Normal file
@ -0,0 +1,51 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user