Your SlideShare is downloading. ×
Hashing
Hashing
Hashing
Hashing
Hashing
Hashing
Hashing
Hashing
Hashing
Hashing
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Hashing

289

Published on

Courtesy: Sir Nabeel Sabir's Share Folder

Courtesy: Sir Nabeel Sabir's Share Folder

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
289
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • End of lecture 41. Start of lecture 42.
  • Transcript

    • 1. Implementation 6: Hashing  An array in which TableNodes are not stored key entry consecutively  Their place of storage is 4 calculated using the key and a hash function 10 hash array Key index function 123  Keys and entries are scattered throughout the array.
    • 2. Hashing insert: calculate place of storage, insert key entry TableNode; (1) find: calculate place of 4 storage, retrieve entry; (1) 10 remove: calculate place of storage, set it to null; (1) 123 All are constant time (1) !
    • 3. Hashing  We use an array of some fixed size T to hold the data. T is typically prime.  Each key is mapped into some number in the range 0 to T-1 using a hash function, which ideally should be efficient to compute.
    • 4. Example: fruits Suppose our hash function 0 kiwi gave us the following 1 values: 2 banana hashCode("apple") = 5 3 watermelon hashCode("watermelon") = 3 4 hashCode("grapes") = 8 hashCode("cantaloupe") = 7 5 apple hashCode("kiwi") = 0 6 mango hashCode("strawberry") = 9 7 cantaloupe hashCode("mango") = 6 hashCode("banana") = 2 8 grapes 9 strawberry
    • 5. Example  Store data in a table 0 kiwi 1 array: table[5] = "apple" 2 banana table[3] = "watermelon" 3 watermelon table[8] = "grapes" 4 table[7] = "cantaloupe" 5 apple table[0] = "kiwi" table[9] = "strawberry" 6 mango table[6] = "mango" 7 cantaloupe table[2] = "banana" 8 grapes 9 strawberry
    • 6. Example  Associative array: 0 kiwi 1 table["apple"] 2 banana table["watermelon"] table["grapes"] 3 watermelon 4 table["cantaloupe"] table["kiwi"] 5 apple table["strawberry"] 6 mango table["mango"] 7 cantaloupe table["banana"] 8 grapes 9 strawberry
    • 7. Example Hash Functions If the keys are strings the hash function is some function of the characters in the strings. One possibility is to simply add the ASCII values of the characters:  length −1  h( str ) =  ∑ str[i ] %TableSize    i =0  Example : h( ABC ) = (65 + 66 + 67)%TableSize
    • 8. Finding the hash function int hashCode( char* s ) { int i, sum; sum = 0; for(i=0; i < strlen(s); i++ ) sum = sum + s[i]; // ascii value return sum % TABLESIZE; }
    • 9. Example Hash Functions Another possibility is to convert the string into some number in some arbitrary base b (b also might be a prime number):  length −1 i h( str ) =  ∑ str[i ] × b %T    i =0  Example : h( ABC ) = (65b 0 + 66b1 + 67b 2 )%T
    • 10. Example Hash Functions If the keys are integers then key%T is generally a good hash function, unless the data has some undesirable features. For example, if T = 10 and all keys end in zeros, then key%T = 0 for all keys. In general, to avoid situations like this, T should be a prime number.

    ×