Mike Png
Mike Png

Reputation: 3

Hash Table with Linear Probing and Random Numbers

Hi I have the task to show, and count, the trace of operations needed to insert the randomly generated keys, one by one, into an initially empty 101-bucket hash table.

I have created this class to generate 100 random numbers and then insert them into the hash table class, but i dont know how to insert them to the hash table and i do it manually.
I will appreciate if you give me some advice.

The Code:

import java.util.Random;

public class RandomNumbers {
static void main(String[] args) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}




public RandomNumbers() {


    for (int i = 0; i < 100; i++) {
        System.out.println(getRandomNumberInRange(0, 15024267));
    }

}


public static int getRandomNumberInRange(int min, int max) {

    if (min >= max) {
        throw new IllegalArgumentException("max must be greater than min");
    }

    Random r = new Random();
    return r.nextInt((max - min) + 1) + min;
}

    String[] numbersPrinting;

}

As you can see I inserted the random numbers manually.

The Hash Table Code:

import java.util.Arrays;

/**
 *
 *
 */
public class HashFunction {

String[] theArray;
int arraySize;
int itemsInArray = 0;
    int K = 0;

/**
 *
 */
public static void main(String[] args) {

    HashFunction theFunc = new HashFunction(101);


    String[] elementsToAdd2 = {"3638273","3483793","1362909","14908691","14624805","3959017","14208240","8911589","3701879", };


    theFunc.hashFunction2(elementsToAdd2, theFunc.theArray);



}


public void hashFunction2(String[] stringsForArray, String[] theArray) {

    for (int n = 0; n < stringsForArray.length; n++) {

        String newElementVal = stringsForArray[n];

        // Create an index to store the value in by taking
        // the modulus

        int arrayIndex = Integer.parseInt(newElementVal) % 101;

        System.out.println("P" + arrayIndex + " " + "I" + newElementVal + "@" + arrayIndex );

        // Cycle through the array until we find an empty space

        while (theArray[arrayIndex] != "-1") {

            ++arrayIndex;

            System.out.println( "P" + arrayIndex);

            // If we get to the end of the bucket go back to index 0

            arrayIndex %= arraySize;

        }

        theArray[arrayIndex] = newElementVal;

    }

}

HashFunction(int size) {

    arraySize = size;

    theArray = new String[size];

    Arrays.fill(theArray, "-1");

}
}

I don't know how to insert the random numbers from the random numbers class into the Hash Table class

Upvotes: 0

Views: 1130

Answers (1)

Aman Chhabra
Aman Chhabra

Reputation: 3894

I think what you should do is create Random numbers in the main function of HashFunction class:

public static void main(String[] args) {

HashFunction theFunc = new HashFunction(101);

String[] elementsToAdd2 = new String[101];

for (int i = 0; i <= 100; i++) {
    elementsToAdd2[i] = Integer.toString(RandomNumbers.getRandomNumberInRange(0, 15024267));
}

theFunc.hashFunction2(elementsToAdd2, theFunc.theArray);

}

With this way, you will have access to Random Numbers array in HashFunction class and you can add it in theArray array of HashFunction class.

Upvotes: 0

Related Questions