10 Hash Table
10 Hash Table
Programming
(C++)
Hash Table
1
About array ….
▪ Time complexity
▪ Collision
▪ Close hashing
▪ Examples
3
Time complexity
4
Big-O Notation
❑ Introduction
▪ Introduction about Big-O Notation: bit.ly/312x9ou
5
Other Data structure
❑ Searching operation
▪ Linked list
▪ Array
O(n)
6
Hash Table
❑ Searching operation: Time Complexity
7
General rules for time complexity
8
9
10
11
12
13
Terms
❑ Hash Table Vs. Hash Function Vs. Hashing
▪ Hash Table
▪ Is a data structure which is used to store key-value pairs. It is implemented as array
▪ Hash Function
▪ Is a function used by hash table to compute an index of an array in which an element will
be inserted to or be searched at
▪ Hashing
▪ In hashing, large keys are converted into smaller ones using hash function AND then the
values are stored in hash table.
14
Hash Functions
15
An example of a hash function
❑ Definition
16
#include<iostream> Example: Implementation of hash table
using namespace std;
void initializeArray(){ 1 main(){
for(int i=0; i<SIZE; i++){
const int SIZE=7; ht[i] = -999 main(){ insertData(7); 2
int ht[SIZE]; }
} insertData(7); insertData(8);
int hashFunction(int n){ insertData(8); insertData(15);
return n%SIZE; insertData(25); insertData(22);
} displayHT(); insertData(25);
void insertData(int value){ } displayHT();
int index; }
index = hashFunction(value);
ht[index] = value; What is the output?
}
void displayHT(){
for(int i=0; i<SIZE; i++){
cout<<i<<"\t --> ";
cout<<ht[i]<<"\n“’
}
} Ooop! Data is overridden?
This is known as collision!
17
Collision
❑ Definition
18
Factors of a good hash function
✓ Easy to compute
✓ Minimize collision
19
Perfect Hashing
❑ Definition
(no collision)
20
Collision Resolution Techniques
(Array-based implementation)
(An array of linked list implementation)
▪ Open hashing defines each slot in the hash table to be the head of a
linked list
22
Hash Table with Chaining resolution technique (An array of linked list)
1. Linear probing
2. Quadratic probing
3. Double hashing
Case Study
Try these topics and explore how each method works!
24
Q&A
25