14Hashing - CMSC 132: Object-Oriented Programming II...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 132: Object-Oriented Programming II Hashing Department of Computer Science University of Maryland, College Park
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Hashing Hashing Hashing function function that maps data to a value (e.g., integer) Hash Code/Hash Value value returned by a hash function Hash functions can be used to speed up data access We can achieve O(1) data access using hashing Approach Use hash function to convert key into number ( hash value ) used as index in hash table
Background image of page 2
3 Hashing Hash Table Array indexed using hash values Hash table A with size N Indices of A range from 0 to N-1 Store in A[ hashValue % N]
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Hash Function Hash Function Function for converting key into hash value For hash table of size N Must reduce hash value to 0. .N – 1 Can use modulo operator hash value = Math.abs(keyValue % N) Example Problem Assign 4 parking spaces to 4 people using h(key) = keyValue % 4 What happens if we have 4 spaces and 8 people? Collision Same hash value for multiple keys Bucket Each table entry can be referred to as a bucket In some implementations the bucket is represented by a list (those elements hashing to the same bucket are placed in the same list)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

14Hashing - CMSC 132: Object-Oriented Programming II...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online