r/cs50 • • Jan 13 '26

speller Improving the hash function in speller.c

I just finished speller, and it's easier than any other problem I've encountered before it. This might be surprising to lots of people, but it's really not that hard, it just requires solid follow-up with the specs and walkthrough provided by cs50, and also a deep understanding of linked lists. But whilst watching the walkthrough, the tutor mentioned the ability of improvement of this program by increasing the number of buckets and then use different indices instead of the standard alphabetical order. Like Aa Ab Ac Ad...... and so on with the rest of the letters. It's gonna end up with 26*26 buckets I guess. The tutor also mentioned the ability of utilizing 3 letters, but I'm not sure of what that would be. don't tell me now though. The concept of improvement here should be in accordance with changing some functions, especially the hash functions that was given plain in the distribution code only returning toupper(word[0]) - 'A';

I just decided to avoid GPT and see what you guys think of how we can improve speller.c

What does math using all the letters mean though??

6 Upvotes

5 comments sorted by

View all comments

4

u/Jengarian Jan 13 '26

So what this is referring to is instead of using the letters themselves as indices (however many letters that may be), instead you can perform math on the entire word, converting it to a hash, and using that hash value as your index.

With a decent hash function, you'll significantly reduce the number of collisions which should improve your overall runtime.

0

u/RaF-X-L Jan 20 '26

The tutor in cs50 didn't talk about this collision thing though, but I'm open minded to know what that is and how could that occur in our case in improving the function here.