SlideShare a Scribd company logo
1 of 20
Hash Table
Presenting to: Prof M.Arshad
Class: Analysis of Algorithms
Department of Computer Science
Searching
Link List
Array
Trees etc
Aamir sohail 2
Searching: Time Complexity
Link List
O(n)
Array (unsorted)
O(n)
Aamir sohail 3
Searching: Time Complexity
Array (Sorted)
O(log2 n)
Binary Search tree
O(log2 n)
Aamir sohail 4
Searching: Time Complexity
Hash Table
O(1)
Aamir sohail 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.
Aamir sohail 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.
Aamir sohail 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
Aamir sohail 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.
Aamir sohail 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.
Aamir sohail 10
Parameters of good hash function
• Easy to Compute
• Minimize Collision
Aamir sohail 11
Perfect Hashing
• Perfect hashing maps each valid input to
a different hash value ( No Collision )
Aamir sohail 12
Hash function example
int hash(int x)
{
return(x%10);
}
Aamir sohail 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.
Aamir sohail 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.
Aamir sohail 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).
Aamir sohail 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.
Aamir sohail 17
Closed Hashing
• Linear Probing
• Quadratic Probing
• Double Hashing
Aamir sohail 18
Any Question about the
Hashing
Hash Function &
Hash table?
Aamir sohail 19
References
Introduction to Algorithms (Second
Edition)
- by: Thomas H.Cormen
Basics of Data Structure
Video Lectures
Aamir sohail 20

More Related Content

What's hot (20)

Heap sort
Heap sort Heap sort
Heap sort
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Linked List
Linked ListLinked List
Linked List
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
 
Sorting
SortingSorting
Sorting
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Hashing
HashingHashing
Hashing
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Hash table
Hash tableHash table
Hash table
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Stack
StackStack
Stack
 
Hashing
HashingHashing
Hashing
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 

Similar to Hash Table Searching in O(1) Time

358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15sumitbardhan
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithmfarhankhan89766
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniyaTutorialsDuniya.com
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesssuserec8a711
 
Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptxdfsdg3
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxmy6305874
 

Similar to Hash Table Searching in O(1) Time (20)

358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
lecture10.ppt
lecture10.pptlecture10.ppt
lecture10.ppt
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Hashing
HashingHashing
Hashing
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniques
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
Hashing
HashingHashing
Hashing
 
Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptx
 
Presentation1
Presentation1Presentation1
Presentation1
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptx
 
hashing.pdf
hashing.pdfhashing.pdf
hashing.pdf
 
28890 lecture10
28890 lecture1028890 lecture10
28890 lecture10
 

More from Aamir Sohail

Presentation on java servlets
Presentation on java servletsPresentation on java servlets
Presentation on java servletsAamir Sohail
 
Presentation on html, css
Presentation on html, cssPresentation on html, css
Presentation on html, cssAamir Sohail
 
Line clipping algorithm (Detailed)
Line clipping algorithm (Detailed)Line clipping algorithm (Detailed)
Line clipping algorithm (Detailed)Aamir Sohail
 
Infromation securiity
Infromation securiityInfromation securiity
Infromation securiityAamir Sohail
 
Network Security Policies
Network Security PoliciesNetwork Security Policies
Network Security PoliciesAamir Sohail
 
Scheduling and scheduling personnel
Scheduling and scheduling personnelScheduling and scheduling personnel
Scheduling and scheduling personnelAamir Sohail
 

More from Aamir Sohail (7)

Presentation on java servlets
Presentation on java servletsPresentation on java servlets
Presentation on java servlets
 
Presentation on html, css
Presentation on html, cssPresentation on html, css
Presentation on html, css
 
Line clipping algorithm (Detailed)
Line clipping algorithm (Detailed)Line clipping algorithm (Detailed)
Line clipping algorithm (Detailed)
 
Vb script
Vb scriptVb script
Vb script
 
Infromation securiity
Infromation securiityInfromation securiity
Infromation securiity
 
Network Security Policies
Network Security PoliciesNetwork Security Policies
Network Security Policies
 
Scheduling and scheduling personnel
Scheduling and scheduling personnelScheduling and scheduling personnel
Scheduling and scheduling personnel
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 

Hash Table Searching in O(1) Time

  • 1. Hash Table Presenting to: Prof M.Arshad Class: Analysis of Algorithms Department of Computer Science
  • 3. Searching: Time Complexity Link List O(n) Array (unsorted) O(n) Aamir sohail 3
  • 4. Searching: Time Complexity Array (Sorted) O(log2 n) Binary Search tree O(log2 n) Aamir sohail 4
  • 5. Searching: Time Complexity Hash Table O(1) Aamir sohail 5
  • 6. 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. Aamir sohail 6
  • 7. 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. Aamir sohail 7
  • 8. 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 Aamir sohail 8
  • 9. 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. Aamir sohail 9
  • 10. 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. Aamir sohail 10
  • 11. Parameters of good hash function • Easy to Compute • Minimize Collision Aamir sohail 11
  • 12. Perfect Hashing • Perfect hashing maps each valid input to a different hash value ( No Collision ) Aamir sohail 12
  • 13. Hash function example int hash(int x) { return(x%10); } Aamir sohail 13
  • 14. 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. Aamir sohail 14
  • 15. 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. Aamir sohail 15
  • 16. 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). Aamir sohail 16
  • 17. 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. Aamir sohail 17
  • 18. Closed Hashing • Linear Probing • Quadratic Probing • Double Hashing Aamir sohail 18
  • 19. Any Question about the Hashing Hash Function & Hash table? Aamir sohail 19
  • 20. References Introduction to Algorithms (Second Edition) - by: Thomas H.Cormen Basics of Data Structure Video Lectures Aamir sohail 20