Compare commits
3 Commits
1feed10d53
...
f04910cb73
Author | SHA1 | Date | |
---|---|---|---|
f04910cb73 | |||
e0f1af33a1 | |||
a7c4cfa36c |
3
.idea/.gitignore
vendored
3
.idea/.gitignore
vendored
@ -8,4 +8,5 @@
|
|||||||
/dataSources.local.xml
|
/dataSources.local.xml
|
||||||
|
|
||||||
.iml
|
.iml
|
||||||
.xml
|
.xml# GitHub Copilot persisted chat sessions
|
||||||
|
/copilot/chatSessions
|
||||||
|
@ -48,20 +48,16 @@ public class InterpolationSearch {
|
|||||||
/* Checks if high is the same thing as low to prevent division by zero. */
|
/* Checks if high is the same thing as low to prevent division by zero. */
|
||||||
if (high != low) {
|
if (high != low) {
|
||||||
probe = low + (((key - array[low]) * (high - low)) / (array[high] - array[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++;
|
this.divisions++;
|
||||||
} else {
|
} else {
|
||||||
probe = high;
|
probe = high;
|
||||||
if (array[probe] != key) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Checks if it passed the element due to it not being in list. */
|
/* Checks if it passed the element due to it not being in list. */
|
||||||
if (array[high] < key || array[low] > key) {
|
if (array[high] < key || array[low] > key) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Checks if the key is less than the probe's value */
|
/* Checks if the key is less than the probe's value */
|
||||||
if (array[probe] > key) {
|
if (array[probe] > key) {
|
||||||
high = probe - 1;
|
high = probe - 1;
|
||||||
|
@ -9,6 +9,128 @@ public class TestInterpolationSearch {
|
|||||||
/** Holds the size of the table to generate */
|
/** Holds the size of the table to generate */
|
||||||
public int tableSize;
|
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.
|
* Generates a list of 1024 random, non-repeating, integers and assigns them to the values field.
|
||||||
*/
|
*/
|
||||||
@ -37,15 +159,21 @@ public class TestInterpolationSearch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void runIntSearch() {
|
public void runIntSearch() {
|
||||||
int totalDivisions;
|
System.out.println(this.values.length + " " + this.tableSize);
|
||||||
|
int totalDivisions = 0;
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
|
|
||||||
System.out.println(" Key\t\tFound\t\tIndex\t\tDivisions");
|
System.out.println(" " + String.format("%-10s%-10s%-10s%-10s", "Key", "Found", "Index", "Divisions"));
|
||||||
System.out.println("----------------------------------------------");
|
System.out.println("----------------------------------------------");
|
||||||
for (int i = 0; i < this.tableSize; i++) {
|
for (int i = 0; i < this.tableSize; i++) {
|
||||||
int key = rand.nextInt(9999) + 1;
|
int key = rand.nextInt(9999) + 1;
|
||||||
InterpolationSearch results = new InterpolationSearch(this.values, key);
|
InterpolationSearch results = new InterpolationSearch(this.values, key);
|
||||||
System.out.println(" " + key + "\t\t" + results.isFound() + "\t\t" + results.getIndex() + "\t\t" + results.getDivisions());
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user