hash table quadratic probing help please

T

Totti

Hi all, i am trying to do a quadratic hashing but i am having some
difficulties probably because i am not in the right logic.using this
code:

public void insert(DataItem item)
{
int key = item.getKey();
int hashVal = hashFunc(key);
int x = 0;
while(arr[hashVal] != null )
{
hashVal = hashVal + (int) Math.pow(x,2); // go to next cell
hashVal %= size; // wraparound if necessary
x++;
}
arr[hashVal] = item;
}
thi seems working ok untill something has taken the slot meant to be
the item's i am inserting at this moment, so it has to go as x + (1^2)
if empty, if not x+(2^2) *** ^ means to the power of
so on and so forth, the x++ shall occur only if the previous x could
not been committed.
would you please help?
it is wrapping arround but the problem is the x++ i think, i dont know
how to fix it, i am pretty new to java,
i ll appreciate any help and thanks in advance
 
J

jkohen

Hi all, i am trying to do a quadratic hashing but i am having some
difficulties probably because i am not in the right logic.using this
code:

public void insert(DataItem item)
{
int key = item.getKey();
int hashVal = hashFunc(key);
int x = 0;
while(arr[hashVal] != null )
{
hashVal = hashVal + (int) Math.pow(x,2); // go to next cell

I am somewhat guessing, but according to your description below, I
think you always want to use the original hashVal on the right side
expression.

Also, what's wrong with the old x*x?
 
E

Eric Sosman

Totti said:
Hi all, i am trying to do a quadratic hashing but i am having some
difficulties probably because i am not in the right logic.using this
code:

public void insert(DataItem item)
{
int key = item.getKey();
int hashVal = hashFunc(key);
int x = 0;
while(arr[hashVal] != null )

This line assumes that 0 <= hashVal < arr.length, which
may in fact be true but suggests an "unhealthy" coupling
between arr and hashFunc.
{
hashVal = hashVal + (int) Math.pow(x,2); // go to next cell

Three points about this line: First, `x * x' would be a
good deal simpler. Second, x is zero on the first trip through
the loop, so hashVal is unchanged and the first two probes will
go to the same spot in the array. Third, if you make so many
probes that x grows to 46341 you'll find that x*x becomes negative
and messes up the hashVal calculation (probably not a problem
unless the hash table gets completely overstuffed).
hashVal %= size; // wraparound if necessary

I guess size is equal to arr.length, or at any rate is no
larger than arr.length? It would be better to use arr.length
directly.

How certain are you that this probe sequence will eventually
visit every array index? For example, if the array size is eleven
and the first hashVal is zero, locations [2], [4], [7], and [9]
are untouched in the first hundred probes (unless I botched my
quick-and-dirty spreadsheet).
x++;
}
arr[hashVal] = item;
}
thi seems working ok untill something has taken the slot meant to be
the item's i am inserting at this moment, [...]

It's unfortunate that you didn't reveal the manner in
which the code fails. You tell us it's "working ok" in some
conditions, and you tell us the condition where it doesn't
work, but you don't tell us what goes wrong or why you think
it is wrong.
 
R

Roedy Green

thi seems working ok untill something has taken the slot meant to be
the item's i am inserting at this moment, so it has to go as x + (1^2)
if empty, if not x+(2^2) *** ^ means to the power of
so on and so forth, the x++ shall occur only if the previous x could
not been committed.

You could just use a HashMap which handles this for you.

Or you could test the cell for not-null, and if it is, increment,
wrapping around till you find a free slot to put it in.

see http://mindprod.com/jgloss/hashmap.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top