I don't know where is the error (Insert into table). It's fragment of my code (inserting into open addressing hash table). Linear and double addressing are good, but with this (quadratic function addressing) its something wrong
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -848
at openaddresshash.OpenAddressHash.insertKwadratowe(OpenAddressHash.java:101)
at openaddresshash.OpenAddressHash.main(OpenAddressHash.java:261)
Java Result: 1
I know that is something wrong with this line:
int index = ((start + (c1 * i) + (c2 * i * i))) % size;
But in my opinion its everything good becouse my function (index) should be looks like:
h(k,i) = (h'(k) + c1*i + c2*i^2) mod m
where h'(k) = k mod m
My code:
for( int d = 25; d<=2500; d+=25)
{
int liczba=4*d;
OpenAddressHash hstb = new OpenAddressHash(liczba);
int jj=2*d;
hstb.AdresowanieKwadratoweDane(1, jj);
Losowania los = new Losowania(); // random values
los.Losowe(liczba);
for(int yy=0; yy<liczba; yy++)
{
hstb.insertKwadratowe(los.trzy[yy]);//trzy is a table with random values
if((yy%(Math.ceil(liczba/50)))==0)
{
AdresowanieKwadratowe.println( liczba+" "+yy+" "+hstb.s );
}
hstb.s=0;
}
}
static public class SLOT
{
public int key;
public STATUS stat;
public SLOT()
{
stat = STATUS.INVALID;
}
}
public void AdresowanieKwadratoweDane(int c1, int c2)
{
this.c1 = c1;
this.c2 = c2;
}
public OpenAddressHash(int n)
{
table = new SLOT[n];
for (int i = 0; i < table.length; i++)
{
table[i] = new SLOT();
}
}
public int insertKwadratowe(int key)
{
int size = table.length;
int start = key%size;
for (int i = 0; i < size; i++)
{
s++;
int index = ((start + (c1 * i) + (c2 * i * i))) % size;
if (table[index].stat == STATUS.INVALID ||
table[index].stat == STATUS.DELETED)
{
table[index] = new SLOT();
table[index].key = key;
table[index].stat = STATUS.OCCUPIED;
return index;
}
}
return -1;
}
public void AdresowanieKwadratoweDane(int c1, int c2)
{
this.c1 = c1;
this.c2 = c2;
}
index. Hard to figure out, not knowing what's really going on in your code.