Added headers and fixed the loops/prints. Added Comments.

This commit is contained in:
Jonathan Turner 2024-03-23 17:47:02 -04:00
parent f04910cb73
commit d0a47f7b64
2 changed files with 19 additions and 3 deletions

View File

@ -1,3 +1,9 @@
// Name: Jonathan Turner
// Class: CS 4306/01
// Term: Spring 2024
// Instructor: Dr. Haddad
// Assignment: 3
// IDE Name: IntelliJ
package Assignments.A3;
public class InterpolationSearch {

View File

@ -1,3 +1,9 @@
// Name: Jonathan Turner
// Class: CS 4306/01
// Term: Spring 2024
// Instructor: Dr. Haddad
// Assignment: 3
// IDE Name: IntelliJ
package Assignments.A3;
import java.util.*;
@ -103,14 +109,14 @@ public class TestInterpolationSearch {
System.out.println();
} else if (option == 2) { /* Handles the table size */
System.out.println("Enter the table size: ");
System.out.print("Option: ");
System.out.print("Size: ");
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: ");
System.out.print("Size: ");
size = getIntegerInput();
}
this.tableSize = size;
@ -159,18 +165,22 @@ public class TestInterpolationSearch {
}
public void runIntSearch() {
System.out.println(this.values.length + " " + this.tableSize);
int totalDivisions = 0;
Random rand = new Random();
/* Prints out the table headers */
System.out.println(" " + String.format("%-10s%-10s%-10s%-10s", "Key", "Found", "Index", "Divisions"));
System.out.println("----------------------------------------------");
/* Prints out each row and performs the Interpolation Search for each value up to the table size. */
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();
}
/* Prints out the average and the difference from the expected average */
double averageDivs = (double) totalDivisions / (double) this.tableSize;
System.out.println("\nDivisions average: " + averageDivs);
System.out.println("Difference: " + Math.abs(3.322 - averageDivs));