Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Doubling hashing, infinite loop?

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Doubling hashing, infinite loop?

    Hello JPF,

    I am trying to implement a hash table, using the methods of double hashing. It seems that I am running an infinite loop. Any suggestions on how I can fix this? Thanks.

    private int find(Object key) 
      {
         // Calculate the starting index.
         int index = key.hashCode() % table.length;
         if (index < 0)
           index += table.length; // Make it positive.
     
           // Increment index until an empty slot is reached
           // or the key is found.
         while ( (table[index] != null)&& (!key.equals(table[index].key))) 
         {
           index= 1 + (Math.abs(key.hashCode())%(table.length-2));
           probes++;
           // Check for wraparound.
           if (index >= table.length)
             index = (index+1)%table.length; // Wrap around.
         }
         return index;
       }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Doubling hashing, infinite loop?

    The problem with Double Hashing is that you can get a zero increment. You can read Wikipedia's description of Double Hashing for a way to try and solve this issue. You may also want to check to see if the item is not in the hash table. The last issue you can encounter with hashing is that you'll end up with "skipped" spots.
    ex:
    Say you had a hash table of size 10, and the double hashing happens to have an increment of 2. Then, all of the odd values would never get checked.

    The solution to the last problem is to only have hash tables of sizes which are prime numbers.

Similar Threads

  1. Do While Loop
    By connex in forum Loops & Control Statements
    Replies: 6
    Last Post: December 7th, 2009, 02:54 PM
  2. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 01:10 PM
  3. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  4. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM