Hash Table Presented by Mrs T Kavipriya and

Hash Table Presented by Mrs. T. Kavipriya and Ms. G. priyanka Hindusthan CAS

Searching Link List Array Trees 2

Searching: Time Complexity Link List O(n) Array (unsorted) O(n) 3

Searching: Time Complexity Array (Sorted) O(log 2 n) Binary Search tree O(log 2 n) 4

Searching: Time Complexity Hash Table O(1) 5

Example 1 • To store the key/values pair, we can use simple array like data structure where keys can be used as index to store values. 6

Example 2 • We have 10 complaints indexed with a number ranges from 0 to 9 ( say complaint number) • We have an array of 10 spaces to store 10 complaints • Here we can use complaint number as key and it is nothing but index number in an array. 7

Example 3 • We have 5 students to store in table (nothing but a custom array). Student will be assigned a Roll no, so roll no is the key value to identify a student. • Assume that the table (array) has 10 slots available. • Also assume that a roll no is a 4 digit number 8

Terms • Hash Table - Mostly it is an array to store dataset. • Hash Function -a hash function is any function that can be used to map dataset. • Hashing -in hashing large keys are converted in to small ones by using hash function and then the values are stored in hash table. 9

Hash Functions • A hash function usually means a function that compresses. Meaning the output is shorter than the input. • A hash function is any function that can be used to map data of arbitrary size to data of fixed size. 10

Parameters of good hash function • Easy to Compute • Minimize Collision 11

Perfect Hashing • Perfect hashing maps each valid input to a different hash value ( No Collision ) 12

Hash function example int hash(int x) { return(x%10); } 13

Hash Function • A good hash function to use with integer key values is the mid-square method • The mid-square method squares the key value, and then takes out the middle r bits of the result. 14

Collision • A situation when the resultant hashes for two or more data elements in data set, maps to the same location in the table, is called a hash collision. • In such situation two or more data elements would qualify to be stored/mapped to the same location in hash table. 15

Solution for Hash Collision • Two types of solutions for hash collision - Open Hashing (chaining) - Closed Hashing (open Addressing) • The difference between the two has to do with - whether collisions are stored outside the table (open hashing), or - whether collisions result in storing one of the records at another slot in the table (closed hashing). 16

Open Hashing • The simplest form of open hashing defines each slot in the hash table to be the head of linked list. • All records that hash to a particular slot are placed on that slot’s linked list. 17

Closed Hashing • Linear Probing • Quadratic Probing • Double Hashing 18
- Slides: 18