Added headers and fixed the loops/prints. Added Comments.
This commit is contained in:
parent
f04910cb73
commit
d0a47f7b64
@ -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;
|
package Assignments.A3;
|
||||||
|
|
||||||
public class InterpolationSearch {
|
public class InterpolationSearch {
|
||||||
|
@ -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;
|
package Assignments.A3;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -103,14 +109,14 @@ public class TestInterpolationSearch {
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
} else if (option == 2) { /* Handles the table size */
|
} else if (option == 2) { /* Handles the table size */
|
||||||
System.out.println("Enter the table size: ");
|
System.out.println("Enter the table size: ");
|
||||||
System.out.print("Option: ");
|
System.out.print("Size: ");
|
||||||
int size = -1;
|
int size = -1;
|
||||||
size = getIntegerInput();
|
size = getIntegerInput();
|
||||||
|
|
||||||
/* Used if the table input is invalid */
|
/* Used if the table input is invalid */
|
||||||
while (size < 1) {
|
while (size < 1) {
|
||||||
System.out.println("Enter a valid table size: (size > 0)");
|
System.out.println("Enter a valid table size: (size > 0)");
|
||||||
System.out.print("Option: ");
|
System.out.print("Size: ");
|
||||||
size = getIntegerInput();
|
size = getIntegerInput();
|
||||||
}
|
}
|
||||||
this.tableSize = size;
|
this.tableSize = size;
|
||||||
@ -159,18 +165,22 @@ public class TestInterpolationSearch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void runIntSearch() {
|
public void runIntSearch() {
|
||||||
System.out.println(this.values.length + " " + this.tableSize);
|
|
||||||
int totalDivisions = 0;
|
int totalDivisions = 0;
|
||||||
Random rand = new Random();
|
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(" " + String.format("%-10s%-10s%-10s%-10s", "Key", "Found", "Index", "Divisions"));
|
||||||
System.out.println("----------------------------------------------");
|
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++) {
|
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(" " + String.format("%-10s%-10s%-10s%-10s", key, results.isFound(), results.getIndex(), results.getDivisions()));
|
System.out.println(" " + String.format("%-10s%-10s%-10s%-10s", key, results.isFound(), results.getIndex(), results.getDivisions()));
|
||||||
totalDivisions += results.getDivisions();
|
totalDivisions += results.getDivisions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Prints out the average and the difference from the expected average */
|
||||||
double averageDivs = (double) totalDivisions / (double) this.tableSize;
|
double averageDivs = (double) totalDivisions / (double) this.tableSize;
|
||||||
System.out.println("\nDivisions average: " + averageDivs);
|
System.out.println("\nDivisions average: " + averageDivs);
|
||||||
System.out.println("Difference: " + Math.abs(3.322 - averageDivs));
|
System.out.println("Difference: " + Math.abs(3.322 - averageDivs));
|
||||||
|
Loading…
Reference in New Issue
Block a user