Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9127197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:13:42+00:00 2026-06-17T07:13:42+00:00

stper** pages; int tableSize; struct Person{ string name; int age; string homeTown; }; void

  • 0
stper** pages;
int tableSize;    
struct Person{

    string name; 
    int age;    
    string homeTown;
};


void fonk1 (int numberOfBuckets)
{
    pages = new stper*[numberOfBuckets]();
    tableSize = numberOfBuckets;
} 

   int hashPerson(Person& person)
   {
    int hashVal = 0;
    for (int i=0; i < (person.getName()).length() ; i++)
        hashVal = 37*hashVal + (person.getName())[i];

    for (int i=0; i < (person.getHomeTown()).length() ; i++)
        hashVal = 37*hashVal + (person.getHomeTown())[i];   
    hashVal+= person.getAge();  

    hashVal %= tableSize;
    if(hashVal < 0)
        hashVal += tableSize;
    return hashVal;
   }

Hello everyone, I am new at hashing. My hashing function is at above in hashPerson function and asyou can see there are three key. Is my function a good algorithm for hashing and how can I improve the function and decrease the number of collision? (Please ignore if there are any syntax mistakes)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:13:44+00:00Added an answer on June 17, 2026 at 7:13 am

    I have a few suggestions:

    1. Use unsigned instead of int. In my experience this has proven to perform better, as when the unsigned overflows, it still stays non-negative(otherwise %-ing may lead to big problems – you get a negative index and … a crash) and it also results in reduced collision rate(empirically proven). Also after all the function is supposed to return an index in a table so it is natural for the value to be unsigned – the index can not be negative.

    2. Multiply hashVal by something when adding the age. I would suggest a value larger then any possible age for instance 200.

    3. You never say what is tableSize but I would advice you to use some big(as big as possible) prime number, again to reduce collision rate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to display a long string into multiple pages. My Activity.xml has
I have a viewpager that pages through fragments. My FragmentPagerAdapter subclass creates a new
- (void)viewDidLoad{ [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@VidName ofType:@mov]; NSURL *url =
I have a page adapter with two pages from sherlockfragment, but when I change
I am trying to implement swiping between pages on my Application. I currently have
I'm developing a small app that reads in specific html-pages, re-formats them and then
I have a viewpager with 2 pages, on each fragment i put a button
The following code scrolls the 4 pages just fine. My problem is that the
I am using the following code to show dynamic progress dialog : public void
public class AndroidXMLParsingActivity extends Activity { public int currentPage = 1; public ListView lisView1;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.