Question
SIMPLE JAVA DEBUG (ARRAY INDEX OUT OF BOUNDS) NEEDED. PLEASE HELP, WILL LEAVE GREAT FEEDBACK FOR YOU some background: the program is supposed to insert
SIMPLE JAVA DEBUG (ARRAY INDEX OUT OF BOUNDS) NEEDED. PLEASE HELP, WILL LEAVE GREAT FEEDBACK FOR YOU
some background: the program is supposed to insert numbers (nine batches of 100 random numbers) into a quadratic probing hash table . it does this ten times. then it takes the average amount of probes from the last nine runs (incase ten is cold start) and displays average. i am getting an error in my implementation and idk why. i included my main... please help me debug this and get it up and running!
THANK YOU SO MUCH IF YOU ARE ABLE TO HELP!
HERE IS THE QUADRATIC PROBING IMPLEMENTATION (WHERE THE PROBLEM IS)
class QuadraticProbing {
private static int currentSize;
private static int maxSize;
private static int[] keys;
private static int[] vals;
static int probes = 0;
public int[] table;
public int valueCount = 0;
public boolean resizing = false;
// Number of traversals
public int traversals = 0;
/** Constructor **/
public QuadraticProbing(int capacity) {
currentSize = 0;
maxSize = capacity;
table = new int[maxSize];
vals = new int[maxSize];
}
/**
* Name: hashFunction Desc: Calculate the hash of the given value.
*/
private int hashFunction(int value) {
return (2 * value + 5) % table.length;
}
/**
* Name: loadFactor Desc: Return the load factor of the hash table.
*/
private int loadFactor() {
return (int) valueCount / table.length;
}
/**
* Name: probe Desc: Given a value and whether or not we're looking for an
* empty space or a value this function will return the index cooresponding
* to the desired value using quadratic probing.
*/
private int probe(int value, Boolean deleting) {
// Calculate the hash
int hash = hashFunction(value);
// Set the index = 0 for the quadratic traversal
int index = 0;
// If we're deleting then we're looking for the table entry the
// value exists in
// else we're looking for an empty space
System.out.println("CHECKING1");
if (deleting) {
// While we haven't found the value we're looking for
while (table[hash] != value) {
traversals++;
// Calculate the next key to check using quadratic probing
// c1 = 1/2 and c2 = 1/2
// h(k, i) = (h(k) + i/2 + i^2 / 2) % (length of table)
hash = (int) ((hash + (int) index / 2 + (int) (index * index) / 2) % table.length);
// increment index
index++;
}
} else {
// Same thing as above just looking for an empty space instead
// of the value
while (table[hash] != 0) {
traversals++;
hash = (int) ((hash + (int) index / 2 + (int) (index * index) / 2) % table.length);
index++;
}
}
return hash;
}
/**
* Name: resize Desc: Shrinks the table if the load factor drops below 0.25
* and is greater than 8 elements. Grows the table if the load factor is
* greater than 0.5.
*/
private void resize() {
resizing = true;
// add a probe count in the event of a collision
probes += 1;
int[] tempTable = table;
if (loadFactor() >= 0.5) {
table = new int[tempTable.length * 2];
for (int i = 0; i < tempTable.length; i++) {
if (tempTable[i] != 0) {
insert(tempTable[i]);
}
}
} else if (loadFactor() <= 0.25 && tempTable.length > 8) {
table = new int[tempTable.length / 2];
for (int i = 0; i < tempTable.length; i++) {
if (tempTable[i] != 0) {
insert(tempTable[i]);
}
}
}
resizing = false;
}
/**
* Name: insert Desc: Given an integer value calculate it's hash and insert
* it into the table at the appropriate location.
*/
public int insert(int value) {
int hash = probe(value, false);
// add a probe count for insertion
probes += 1;
table[hash] = value;
if (!resizing) {
valueCount++;
resize();
}
return hash;
}
public static int probe_counter() {
return probes;
}
/** Function to reset the probe counter to equal zero **/
static int reset_probe_counter() {
probes = 0;
return probes;
}
}
HERE IS MY CURRENT MAIN:
import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Scanner;
// ----------------------------------------------------------------
// THE FOLLOWING IS FOR QUADRATIC PROBING WITH PRIME SIZE (QDR-p)
// ----------------------------------------------------------------
// reset probes counter so they don't still have data from last run.
// Other than that, the only difference is hash table size
// changes from 1000 to 1009 (non prime to prime)
probes_b1 = 0;
probes_b2 = 0;
probes_b3 = 0;
probes_b4 = 0;
probes_b5 = 0;
probes_b6 = 0;
probes_b7 = 0;
probes_b8 = 0;
probes_b9 = 0;
for (int j = 0; j < 9; j++) {
// make the random list with specifications from homework sheet
for (int i = 0; i < 1000; i++) {
num = ThreadLocalRandom.current().nextInt(0, 99999);
list[i] = num;
}
int[] keys = Arrays.copyOfRange(list, 0, 899);
QuadraticProbing QDR_p = new QuadraticProbing(1009);
for (int i = 0; i < 100; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b1 += probe_count;
QDR_p.reset_probe_counter();
}
// second batch
for (int i = 100; i < 199; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b2 += probe_count;
QDR_p.reset_probe_counter();
}
// third batch
for (int i = 200; i < 299; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b3 += probe_count;
QDR_p.reset_probe_counter();
}
// fourth batch
for (int i = 300; i < 399; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b4 += probe_count;
QDR_p.reset_probe_counter();
}
// fifth batch
for (int i = 400; i < 499; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b5 += probe_count;
QDR_p.reset_probe_counter();
}
// sixth batch
for (int i = 500; i < 599; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b6 += probe_count;
QDR_p.reset_probe_counter();
}
// seventh batch
for (int i = 600; i < 699; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b7 += probe_count;
QDR_p.reset_probe_counter();
}
// eighth batch
for (int i = 70; i < 799; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b8 += probe_count;
QDR_p.reset_probe_counter();
}
// ninth batch
for (int i = 800; i < 899; i++) {
QDR_p.insert(keys[i]);
}
if (j > 0) {
int probe_count = QDR_p.probe_counter();
probes_b9 += probe_count;
QDR_p.reset_probe_counter();
}
}
System.out.println("DATA FOR QDR-p");
System.out.println("-----------------------------------------------------------");
float avg_b1q = probes_b1 / 9;
System.out.println("The average number of probes from the runs for batch #1 is : ");
System.out.format("%.2f", avg_b1q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
float avg_b2q = probes_b2 / 9;
System.out.println("The average number of probes from the runs for batch #2 is : ");
System.out.format("%.2f", avg_b2q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
float avg_b3q = probes_b3 / 9;
System.out.println("The average number of probes from the runs for batch #3 is : ");
System.out.format("%.2f", avg_b3q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
float avg_b4q = probes_b4 / 9;
System.out.println("The average number of probes from the runs for batch #4 is : ");
System.out.format("%.2f", avg_b4q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
float avg_b5q = probes_b5 / 9;
System.out.println("The average number of probes from the runs for batch #5 is : ");
System.out.format("%.2f", avg_b5q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
float avg_b6q = probes_b6 / 9;
System.out.println("The average number of probes from the runs for batch #6 is : ");
System.out.format("%.2f", avg_b6q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
float avg_b7q = probes_b7 / 9;
System.out.println("The average number of probes from the runs for batch #7 is : ");
System.out.format("%.2f", avg_b7q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
float avg_b8q = probes_b8 / 9;
System.out.println("The average number of probes from the runs for batch #8 is : ");
System.out.format("%.2f", avg_b8q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
float avg_b9q = probes_b9 / 9;
System.out.println("The average number of probes from the runs for batch #9 is : ");
System.out.format("%.2f", avg_b9q);
System.out.print(" ");
System.out.println("-----------------------------------------------------------");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started