Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 2 Hashing.

Similar presentations


Presentation on theme: "Computer Science 2 Hashing."— Presentation transcript:

1 Computer Science 2 Hashing

2 Learning Objectives Understand the benefits of hashing
Be able to generate a value based a string Be able to write a function to generate a hashed value. Add information into an array based on Hashed their hashed value.

3 Welcome Back Last week… Hashing Benefits Deficits Why What is hashing?
What do you recall about the algorithm we used last class to find a hashed value for a string?

4 Hashing Function Review: Using Base 2
Find the Ordinal value for each character and subtract 64 so that ‘A’ = 1, ‘B’ = 2, ‘C’=3, … Ord(upcase(stringVariable[position])) – 64 Use Base 2 for the place values, like in Binary Numbers. AB = *2^1 + 2* 2^0 = = 4 BA = *2^1 + 1* 2^0 = = 5 CAB = 2*2^2 + 1*2^1 + 2* 2^0 = =12 Then MOD by this size of the array. Hands on with Algorithm For each of the following, find the hashed value. Use an array size of 5.  Names Value Value MOD 5 AC _______ ________ JC ________ ________ EG ________ ________ BIL ________ ________

5 What about the array? How big should you make the array?
How do you initialize the array? How do you put elements into the array? What if there is someone already at the position? How do you remove someone from the array?

6 Add the values from the Warm up into an array.
PSEUDOCODE: ADD Get info Hash the key field {The name} Found = false {‘Cause you haven’t found where to put it.} count = 0 {Counting to make sure time isn’t wasted when the array is full.} repeat if currentname (theArray[hashedValue].name) = ‘Empty’ or Deleted put in the info at this spot found = true {‘Cuz you’ve found where to put it} else if name = currentname Tell the user “It already exists” found = true {You found the person} else rehash add one to count until (found) or (count is too big) Add the values from the Warm up into an array. Use +2 for rehashing.

7 PSEUDOCODE SHOW ONE Get info Hash the key field {The name}
Found = false {‘Cause you haven’t found where to put it.} count = 0 {Counting to make sure time isn’t wasted when the array is full.} repeat if currentname (theArray[hashedValue].name) = ‘Empty’ Show “The person is not in the list” found = true {‘Cuz you’ve found where to put it} else if name = currentname Show the information about the person found = true {You found the person} else rehash add one to count until (found) or (count is too big)

8 PSEUDOCODE: DELETE ONE
Get info Hash the key field {The name} Found = false {‘Cause you haven’t found where to put it.} count = 0 {Counting to make sure time isn’t wasted when the array is full.} repeat if currentname (theArray[hashedValue].name) = ‘Empty’ Show “The person is not in the list” found = true {‘Cuz you’ve found where to put it} else if name = currentname //What if someone had to rehash from this spot? Delete the person (Copy ‘Deleted’ to the name field) found = true {You found the person} else rehash add one to count until (found) or (count is too big) Dry run with the warm up. Delete JC.

9 Assume all capital letters for this activity.
Dry Running the Pseudo Code:


Download ppt "Computer Science 2 Hashing."

Similar presentations


Ads by Google