wavec022

hash tables notes

Apr 6th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. Hash Tables
  2.  
  3. Big Theta (NOT O) tends towards 1 if we do the hashtable well
  4.  
  5. rethink how we put stuff into it; hashtables are bad at ordering or sorting data
  6.  
  7. A hash table combines two things-- hash function and an array that can store data
  8. run our data through the hash function
  9. store data in the element of the array represented by the returned hash code
  10.  
  11. for example: hashcode(tank) = 4
  12. so we store tank in array(4)
  13. and then to retrieve it we just do the hash code again and it gives us 4
  14.  
  15. how do we define a hash function?
  16. good hash functions exist... use them
  17. Scala has hashCode x.hashCode
  18.  
  19. =====
  20.  
  21. What do I do if there is a collision? when two pieces of data yield the same hashcode
  22. We want to store both
  23. Need a way to get both into table and still maintain quick insertion and lookup
  24.  
  25. Helps to mod by length of array
  26.  
  27. Solution: linear probing-- move to the right and whatnot
  28. Only problem is it causes clustering
  29.  
  30. What if each array element was the head of a linked list? This is called chaining
Add Comment
Please, Sign In to add comment