Compare commits

..

No commits in common. "f04910cb73bebd5398e02f0a5ab6f8bc7c48044e" and "1feed10d53187d596c82c6fcfa0d1cc8fb452fb2" have entirely different histories.

3 changed files with 9 additions and 134 deletions

3
.idea/.gitignore vendored
View File

@ -8,5 +8,4 @@
/dataSources.local.xml
.iml
.xml# GitHub Copilot persisted chat sessions
/copilot/chatSessions
.xml

View File

@ -48,16 +48,20 @@ public class InterpolationSearch {
/* 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;
if (array[probe] != key) {
break;
}
}
/* Checks if it passed the element due to it not being in list. */
if (array[high] < key || array[low] > key) {
break;
}
/* Checks if the key is less than the probe's value */
if (array[probe] > key) {
high = probe - 1;

View File

@ -9,128 +9,6 @@ public class TestInterpolationSearch {
/** Holds the size of the table to generate */
public int tableSize;
public static void main(String[] args) {
TestInterpolationSearch program = new TestInterpolationSearch();
program.start();
}
/**
* Used as the main sentential loop and loops until the exit option is selected.
*
* @precondition none
* @postcondition none
*/
public void start() {
int option = this.getOption();
while (option != 4) {
this.executeOption(option);
option = this.getOption();
}
}
/**
* 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();
}
/**
* 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;
}
/**
* 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.
*/
protected int getIntegerInput() {
Scanner sc = new Scanner(System.in);
try {
String textInput = sc.nextLine();
return Integer.parseInt(textInput);
} catch (Exception e) {
return -1;
}
}
public void executeOption(int option) {
/* The option used for the values generation */
if (option == 1) {
this.randomDistinct();
System.out.println("The generated Values: ");
/* Displays the generated values */
for (int i = 0; i < this.values.length; i++) {
System.out.print(String.format("%-6s", this.values[i]));
/* Splits the rows at 30 columns */
if ((i+1) % 30 == 0) {
System.out.println();
}
}
System.out.println();
} else if (option == 2) { /* Handles the table size */
System.out.println("Enter the table size: ");
System.out.print("Option: ");
int size = -1;
size = getIntegerInput();
/* Used if the table input is invalid */
while (size < 1) {
System.out.println("Enter a valid table size: (size > 0)");
System.out.print("Option: ");
size = getIntegerInput();
}
this.tableSize = size;
} else if (option == 3) { /* Generates and displays the table */
if (this.values == null) { /* Used to check if the values have been generated */
System.out.println("Please generate the values first.");
System.out.println();
return;
}
if (this.tableSize < 1) { /* Used to check if the table size has been set */
System.out.println("Please enter a table size.");
System.out.println();
return;
}
/* Runs the search */
this.runIntSearch();
}
System.out.println();
}
/**
* Generates a list of 1024 random, non-repeating, integers and assigns them to the values field.
*/
@ -159,21 +37,15 @@ public class TestInterpolationSearch {
}
public void runIntSearch() {
System.out.println(this.values.length + " " + this.tableSize);
int totalDivisions = 0;
int totalDivisions;
Random rand = new Random();
System.out.println(" " + String.format("%-10s%-10s%-10s%-10s", "Key", "Found", "Index", "Divisions"));
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(" " + String.format("%-10s%-10s%-10s%-10s", key, results.isFound(), results.getIndex(), results.getDivisions()));
totalDivisions += results.getDivisions();
}
double averageDivs = (double) totalDivisions / (double) this.tableSize;
System.out.println("\nDivisions average: " + averageDivs);
System.out.println("Difference: " + Math.abs(3.322 - averageDivs));
System.out.println();
System.out.println(" " + key + "\t\t" + results.isFound() + "\t\t" + results.getIndex() + "\t\t" + results.getDivisions());
}
}
}