















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















docsity.com
If the value isn’t there, we need to search all n elements If the value is there, we search n/2 elements on average
A binary search requires O(log n) time About equally fast whether the element is found or not
How about an O(1), that is, constant time search? We can do it if the array is organized in a particular way
docsity.com
hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("cantaloupe") = 7 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2
docsity.com
robin sparrow hawk seagull
bluejay owl
robin info sparrow info hawk info seagull info
bluejay info owl info
docsity.com
hash("apple") = 5 hash("watermelon") = 3 hash("grapes") = 8 hash("cantaloupe") = 7 hash("kiwi") = 0 hash("strawberry") = 9 hash("mango") = 6 hash("banana") = 2 hash("honeydew") = 6
docsity.com
docsity.com
hashCode(seagull) = 143
table[143] != seagull
table[144] != seagull
docsity.com
hashCode(seagull) = 143
table[143] != seagull
table[144] != seagull
docsity.com
hashCode(hawk) = 143
table[143] != hawk
table[144] == hawk
docsity.com
hashCode(cardinal) = 147
docsity.com
docsity.com
Since we may need to rehash many times, we need an easily computable sequence of functions
Probably better if the length of the string was not a component in computing the original hash function
Problem: are we sure we will look at every location in the array?
docsity.com
docsity.com
It can’t use random numbers, or the time of day
Must be consistent with your equals method
docsity.com