Download presentation
Presentation is loading. Please wait.
Published byΒασίλης Λειβαδάς Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
2
Search Linear Search: O(N) Binary Search: O(log n) Any better search algorithm? Is O(1) possible? Hashing! Quiz is coming? Program due! 2 2 2
3
Hashing Function A function from an object to an integer (as array index) O(1) Quiz is coming? Program due! 3 3 3
4
obj Hashing function h O(1) Where to store the object? index = h(obj)
Quiz is coming? Program due! Where is the object? index = h(obj) 4 4 4
5
Hashing Function I Employee [] employees = new Employee[100]; // at most 100 employees // IDs are unique // all IDs are between 1 and 100 Hashing function: index = h(id) = id - 1 Perfect hashing function! Not feasible in many cases. Quiz is coming? Program due! 5 5 5
6
Hashing Function II (Division)
Employee [] employees = new Employee[MAX_SIZE]; // MAX_SIZE is a large prime number Hashing function: index = h(SSN) = SSN % MAX_SIZE Collision! MAX_SIZE: Quiz is coming? Program due! 6 6 6
7
Integer Division No Calculators on Quiz6, Test3, and Final!
20 % % % % 17 3 16 13 1
8
Hashing Function III Employee [] employees = new Employee[MAX_SIZE]; // MAX_SIZE is a large prime number // The key field is a string Hashing function: index = h(key) sum = 0 for each char ch in key sum = sum + int(ch) sum = sum * ANOTHER_PRIME sum = sum % MAX_SIZE Still Collision! Quiz is coming? Program due! 8 8 8
9
Handling Collision Linear Probe (Open Addressing):
Go to the next available slot (circular) Double Hashing: Use a second hashing function if collision occurs (circular) Buckets Each array slot points to an array (2-D array) Quiz is coming? Program due! 9 9 9
10
Example We will assume the division method of hashing: key is an integer table_size: 11 (prime number) Index = h(key) = key % table_size = key % 11 Quiz is coming? Program due! 10 10 10 10
11
Linear Probe Index = key % 11 Index ++ when occupied (circular)
8 Key: 23; index: 23 % 11 = 1 23 8 Key: 32; index: 32 % 11 = 10 Key value 8 23 32 45 55 75 86 Hash value 8 1 10 9 23 8 32 Key: 45; index: 45 % 11 = 1 2 23 45 8 32 Key: 55; index: 55 % 11 = 0 Quiz is coming? Program due! 55 23 45 8 32 Key: 75; index: 75 % 11 = 9 55 23 45 8 75 32 Key: 86; index: 86 % 11 = 9 10 0 1 2 3 55 23 45 86 8 75 32 11
12
Double Hashing Friday
13
Buckets 1 2 3 4 5 6 7 8 9 10 55 Index = key % 11 Hash value 8: 8%11 8 23: 23%11 1 32: 32%11 10 45: 45%11 1 55: 55%11 0 75: 75%11 9 86: 86%11 9 23 45 Quiz is coming? Program due! 8 75 86 32 13 13 13
14
Quiz 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.