SlideShare a Scribd company logo
Searching and Hashing
•Hash Table
• Hash Functions
• Collision Resolution Strategies
• Hash Table Implementation
Hashing
• The searching time of Linear and binary searching techniques
depends on the number of elements.
• Hashing is a search technique, its searching time does not depend
on the number of elements.
• Hashing technique is a search technique in which the required
record is located by using a function. Search time is independent
of the position of the record in the file. The function used to
locate the record is called the hash function.
• A hash function h transforms a key K into a table index L at which
the record with key K is placed and h(K) is called the hash of key
K.
h(K) = k  L
• Hash functions
• The main criteria for the selection of a hash function are
– it should be easy and quick to compute
– should produce an even distribution of keys across the range of indices
– should produce distinct indices
Hashing• There are several basic methods that can be used to
build a hash function.
• Division
An integer key is divided by the table size and the
remainder is taken as the hash value.
Hash value = (key) mod (table_size)
or
Hash value = (key) mod (table_size) + 1
The second one starts the hash value from 1 instead of 0
Best hash values are obtained when table_size is a
prime.
Truncation
Part of the key is ignored and the remaining portion is
used as the index. The method, though simple, fails to
give uniform distribution.
Ex: Given a key of seven digits, then the first, fourth and
seventh digits can make hash function so that the key
2345678 maps to 258.
Hashing
• Folding
The key is divided into several parts and the parts are
combined in a convenient way to get the index. Often
addition or multiplication is used for combining the parts.
This process, termed folding, makes use of all the
information in the key and hence can produce better
distribution of the indices.
Ex: Given a key of seven digits can be divided into groups of
three, two and two digits, the groups are added and the
result according to requirement can be used as such or
processed further.
2345678 maps to 234+56+78 = 368.
• Midsquare method
The key is multiplied by itself and the middle few digits of
the square is taken as index. The number of middle digits to
be taken is dependent on the number of digits allowed in
the index. Since the middle digits of a square is dependent
on all the digits in the key, the chances of keys hashing into
same indices are expected to be small.
Hashing
• Hash collision
• Given a set of keys k1, k2, ….kn a perfect hash function is defined as
one wherein hash-value of ki is not equal to hash value of kj for all
distinct i and j.
• Some times more than one distinct keys give the same hash value.
This is called hash collision or hash clash. This situation is resolved
in several ways.
• Linear probing or linear open
• The simplest method of resolving hash clashes is to search the table
sequentially for the desired key or the empty location. The search is
started from the location the collision occurs. The colliding record is
placed in the next available space. The storage space is considered
as a circular linear space so that when the last location is reached
the search goes to the first location. The method is called linear
probing because of the linear nature of searching.
Hashing
Hashing
• Rehashing or double hashing
• In the method called rehashing a secondary
hash function is used on the hash key. The
hash value is used as input to the rehash
function and a new hash value is computed.
The rehash function is used successively until
a distinct hash value is resulted.
Hashing
6 6
Put on 3rd pos.
from 5th pos.
Put on 5th pos.
from 6th pos.
Hashing
• Quadratic probing
• This approach tries to correct the clustering problem
of linear probing by introducing a quadratic
increment function. Probing is done at locations
given by
( Hash value + j2 ) mod (table_ size) with
j=1,2,3………..
• Quadratic probing reduces clustering considerably
but all the locations are not probed by this method.
When table_size is a prime almost half of the
locations are probed. But if the table_size is a power
of two, relatively few locations are probed.
Hashing
Hashing
• Hashing with buckets
• In this approach multiple keys are hashed to a single
location. The locations are slotted to contain more than
one key. Each of this multi-key location is called a
bucket. Each of these buckets can hold multiple entries
up to a point. This approach allows multiple entries to
hash at the same location. When the bucket is full
collisions are to be handled again.
• Chaining
• In this method called chaining, a linked list of all items
whose key hash into the same value is built. During the
search hash function is first applied to the key and then
the linked list, called chain, is searched sequentially for
the target key. In this technique an extra link field is
added to each table position.
Hashing
Hashing
• There are several advantages by this approach.
• Considerable space is saved when the records are large.
Since hash table is an array and the array space is allocated
at the time of compilation, considerable amount of space is
wasted if some array elements are not occupied. As the
space required for pointers are small, the space wasted will
not be much even if the space allocated remains empty.
• Adding a link to the record and organizing all the records
with a single hash address as a linked list handle collision.
Good hash function will give short linked list enabling quick
search. Clustering is prevented as keys with distinct hash
addresses go to different lists.
• The average length of the linked lists remain small and the
efficiency of the sequential search of the lists is maintained.
• Deletion becomes easy and quick in chained hash table.
Hashing
• There are disadvantages also in the chained hash
table method.
• When the records are small, the space used for
links becomes considerable in comparison with
the space required for storing the records.
• When the hash table is small, there would be
collisions making some of the chains long. This
slows down searching
• However, a good hash function minimizes the
collision and spreads the records uniformly
throughout the file. Larger the range of hash
functions less chances of hash clashes. This
involves the trade-offs between time and space.
Hashing
• Hashing facilitates direct access to a table. For
this reason this scheme is preferable to other
search techniques. The biggest draw back in this
scheme is that the records in a hash table are
not stored in the sorted order of keys.
• They do not minimize hash collisions and hence
cannot access any record directly from its key
thus defeating the basic purpose of hashing.
• In view of speed the hash methods compare
better than other search methods when the size
of the file is large.

More Related Content

What's hot

Marge Sort
Marge SortMarge Sort
Marge Sort
Ankit92Chitnavis
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
rajshreemuthiah
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
Jawad Khan
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMSkoolkampus
 
Hashing data
Hashing dataHashing data
Hashing data
umair khan
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
Chirag vasava
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
NaveenPeter8
 
Dbms normalization
Dbms normalizationDbms normalization
Dbms normalization
Pratik Devmurari
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
Shareb Ismaeel
 
DbMs
DbMsDbMs
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Dbms keys
Dbms keysDbms keys
Dbms keys
RUpaliLohar
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
Kathirvel Ayyaswamy
 
Relational Data Model Introduction
Relational Data Model IntroductionRelational Data Model Introduction
Relational Data Model Introduction
Nishant Munjal
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1Kumar
 
Codd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management SystemsCodd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management Systems
Rajeev Srivastava
 

What's hot (20)

Marge Sort
Marge SortMarge Sort
Marge Sort
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS
 
Hashing data
Hashing dataHashing data
Hashing data
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
 
Dbms normalization
Dbms normalizationDbms normalization
Dbms normalization
 
2-3 Tree
2-3 Tree2-3 Tree
2-3 Tree
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
DbMs
DbMsDbMs
DbMs
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Dbms keys
Dbms keysDbms keys
Dbms keys
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Relational Data Model Introduction
Relational Data Model IntroductionRelational Data Model Introduction
Relational Data Model Introduction
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Data structures
Data structuresData structures
Data structures
 
Codd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management SystemsCodd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management Systems
 

Similar to Hashing

Hashing
HashingHashing
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing Tables
Chinmaya M. N
 
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
ssuserec8a711
 
hashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae java
ishasharma835109
 
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptxunit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
BabaShaikh3
 
Ts project Hash based inventory system
Ts project Hash based inventory systemTs project Hash based inventory system
Ts project Hash based inventory system
DADITIRUMALATARUN
 
Hashing_UNIT2.pptx
Hashing_UNIT2.pptxHashing_UNIT2.pptx
Hashing_UNIT2.pptx
Samatha Korukonda
 
Hash based inventory system
Hash based inventory systemHash based inventory system
Hash based inventory system
DADITIRUMALATARUN
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptx
SLekshmiNair
 
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
my6305874
 
Searching
SearchingSearching
Searching
Ashim Lamichhane
 
Hash tables
Hash tablesHash tables
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
Dr.Shweta
 
Hashing
HashingHashing
Hashing
LavanyaJ28
 
Hash table
Hash tableHash table
Hash table
Vu Tran
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashingRafi Dar
 

Similar to Hashing (20)

Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing Tables
 
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
 
hashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae java
 
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptxunit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
 
Ts project Hash based inventory system
Ts project Hash based inventory systemTs project Hash based inventory system
Ts project Hash based inventory system
 
Hashing_UNIT2.pptx
Hashing_UNIT2.pptxHashing_UNIT2.pptx
Hashing_UNIT2.pptx
 
Hash based inventory system
Hash based inventory systemHash based inventory system
Hash based inventory system
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptx
 
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
 
Searching
SearchingSearching
Searching
 
Hash tables
Hash tablesHash tables
Hash tables
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
Hashing
HashingHashing
Hashing
 
Hash table
Hash tableHash table
Hash table
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
asdfew.pptx
asdfew.pptxasdfew.pptx
asdfew.pptx
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 

More from invertis university

Data link control notes
Data link control notesData link control notes
Data link control notes
invertis university
 
Tree
TreeTree
Sorting
SortingSorting
Program listds
Program listdsProgram listds
Program listds
invertis university
 
Heaps
HeapsHeaps
Dsa book
Dsa bookDsa book
B trees
B treesB trees
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
invertis university
 
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
invertis university
 
System security
System securitySystem security
System security
invertis university
 

More from invertis university (10)

Data link control notes
Data link control notesData link control notes
Data link control notes
 
Tree
TreeTree
Tree
 
Sorting
SortingSorting
Sorting
 
Program listds
Program listdsProgram listds
Program listds
 
Heaps
HeapsHeaps
Heaps
 
Dsa book
Dsa bookDsa book
Dsa book
 
B trees
B treesB trees
B trees
 
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
 
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
 
System security
System securitySystem security
System security
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 

Hashing

  • 1. Searching and Hashing •Hash Table • Hash Functions • Collision Resolution Strategies • Hash Table Implementation
  • 2. Hashing • The searching time of Linear and binary searching techniques depends on the number of elements. • Hashing is a search technique, its searching time does not depend on the number of elements. • Hashing technique is a search technique in which the required record is located by using a function. Search time is independent of the position of the record in the file. The function used to locate the record is called the hash function. • A hash function h transforms a key K into a table index L at which the record with key K is placed and h(K) is called the hash of key K. h(K) = k  L • Hash functions • The main criteria for the selection of a hash function are – it should be easy and quick to compute – should produce an even distribution of keys across the range of indices – should produce distinct indices
  • 3. Hashing• There are several basic methods that can be used to build a hash function. • Division An integer key is divided by the table size and the remainder is taken as the hash value. Hash value = (key) mod (table_size) or Hash value = (key) mod (table_size) + 1 The second one starts the hash value from 1 instead of 0 Best hash values are obtained when table_size is a prime. Truncation Part of the key is ignored and the remaining portion is used as the index. The method, though simple, fails to give uniform distribution. Ex: Given a key of seven digits, then the first, fourth and seventh digits can make hash function so that the key 2345678 maps to 258.
  • 4. Hashing • Folding The key is divided into several parts and the parts are combined in a convenient way to get the index. Often addition or multiplication is used for combining the parts. This process, termed folding, makes use of all the information in the key and hence can produce better distribution of the indices. Ex: Given a key of seven digits can be divided into groups of three, two and two digits, the groups are added and the result according to requirement can be used as such or processed further. 2345678 maps to 234+56+78 = 368. • Midsquare method The key is multiplied by itself and the middle few digits of the square is taken as index. The number of middle digits to be taken is dependent on the number of digits allowed in the index. Since the middle digits of a square is dependent on all the digits in the key, the chances of keys hashing into same indices are expected to be small.
  • 5. Hashing • Hash collision • Given a set of keys k1, k2, ….kn a perfect hash function is defined as one wherein hash-value of ki is not equal to hash value of kj for all distinct i and j. • Some times more than one distinct keys give the same hash value. This is called hash collision or hash clash. This situation is resolved in several ways. • Linear probing or linear open • The simplest method of resolving hash clashes is to search the table sequentially for the desired key or the empty location. The search is started from the location the collision occurs. The colliding record is placed in the next available space. The storage space is considered as a circular linear space so that when the last location is reached the search goes to the first location. The method is called linear probing because of the linear nature of searching.
  • 7. Hashing • Rehashing or double hashing • In the method called rehashing a secondary hash function is used on the hash key. The hash value is used as input to the rehash function and a new hash value is computed. The rehash function is used successively until a distinct hash value is resulted.
  • 8. Hashing 6 6 Put on 3rd pos. from 5th pos. Put on 5th pos. from 6th pos.
  • 9. Hashing • Quadratic probing • This approach tries to correct the clustering problem of linear probing by introducing a quadratic increment function. Probing is done at locations given by ( Hash value + j2 ) mod (table_ size) with j=1,2,3……….. • Quadratic probing reduces clustering considerably but all the locations are not probed by this method. When table_size is a prime almost half of the locations are probed. But if the table_size is a power of two, relatively few locations are probed.
  • 11. Hashing • Hashing with buckets • In this approach multiple keys are hashed to a single location. The locations are slotted to contain more than one key. Each of this multi-key location is called a bucket. Each of these buckets can hold multiple entries up to a point. This approach allows multiple entries to hash at the same location. When the bucket is full collisions are to be handled again. • Chaining • In this method called chaining, a linked list of all items whose key hash into the same value is built. During the search hash function is first applied to the key and then the linked list, called chain, is searched sequentially for the target key. In this technique an extra link field is added to each table position.
  • 13. Hashing • There are several advantages by this approach. • Considerable space is saved when the records are large. Since hash table is an array and the array space is allocated at the time of compilation, considerable amount of space is wasted if some array elements are not occupied. As the space required for pointers are small, the space wasted will not be much even if the space allocated remains empty. • Adding a link to the record and organizing all the records with a single hash address as a linked list handle collision. Good hash function will give short linked list enabling quick search. Clustering is prevented as keys with distinct hash addresses go to different lists. • The average length of the linked lists remain small and the efficiency of the sequential search of the lists is maintained. • Deletion becomes easy and quick in chained hash table.
  • 14. Hashing • There are disadvantages also in the chained hash table method. • When the records are small, the space used for links becomes considerable in comparison with the space required for storing the records. • When the hash table is small, there would be collisions making some of the chains long. This slows down searching • However, a good hash function minimizes the collision and spreads the records uniformly throughout the file. Larger the range of hash functions less chances of hash clashes. This involves the trade-offs between time and space.
  • 15. Hashing • Hashing facilitates direct access to a table. For this reason this scheme is preferable to other search techniques. The biggest draw back in this scheme is that the records in a hash table are not stored in the sorted order of keys. • They do not minimize hash collisions and hence cannot access any record directly from its key thus defeating the basic purpose of hashing. • In view of speed the hash methods compare better than other search methods when the size of the file is large.