Hash Tables

Hash Tables - Hash Tables Hash Table Can we search in worst...

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

View Full Document Right Arrow Icon
Hash Table Can we search in worst case O(1) time? We know that an array lookup at specific spot/index is O(1) time Hash tables work by mapping a key to a value, which is stored in an in-memory data structure. Rather than search through all elements of the hash table for a matching key, a hashing function analyzes a key, and returns an index number. This index matches a stored value, and the data is then accessed. Key -(somehow)-> index into array x y 0 1 2 3 4 x -> 1; f(x) = 1 y -> 3; f(y) = 3 f is identity -> f(k) = k, k in range 1-1000 will generally lead to collisions (2 or more objects map to same index) set up an array of 1001 slots, N = 1001 x null [2|data] null [4|data] 0 1 2 3 4 trade off space against time Hash function - distributes keys uniformly over table, runs in O(1) time enables O(1) best case in search an object by returning the index of the slot the object is in If the keys map to the same spot, use linked list [v | k] 1. h(k) -> i 2. go to table [i]
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Hash Tables - Hash Tables Hash Table Can we search in worst...

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

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