From ba04108bdf7e7dabba6c2e7b25f6e0e7ec85d1ee Mon Sep 17 00:00:00 2001 From: ajay-dhangar <99037494+Ajay-Dhangar@users.noreply.github.com> Date: Sun, 13 Oct 2024 14:59:42 +0000 Subject: [PATCH 1/2] Updated contributors list --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 257a9a623..c70c5a06e 100644 --- a/README.md +++ b/README.md @@ -507,6 +507,15 @@ We are grateful to all the contributors who have helped improve this project. Yo Rachita Dashore + + + rajatsinghal02 +
+ Rajat singhal +
+ + + Kratik1093 @@ -514,8 +523,6 @@ We are grateful to all the contributors who have helped improve this project. Yo Kratik Mandloi - - meghanakn22 @@ -551,6 +558,8 @@ We are grateful to all the contributors who have helped improve this project. Yo subashree + + sejals23 @@ -558,8 +567,6 @@ We are grateful to all the contributors who have helped improve this project. Yo Sejal - - RanaJay3101 @@ -567,13 +574,6 @@ We are grateful to all the contributors who have helped improve this project. Yo Rana Jay - - - rajatsinghal02 -
- Rajat singhal -
- Rahul7raj From 85d2de9a361271bbd00923f014799bbc3c6de264 Mon Sep 17 00:00:00 2001 From: Ananya Gupta <145869907+ananyag309@users.noreply.github.com> Date: Sun, 13 Oct 2024 20:31:03 +0530 Subject: [PATCH 2/2] Add files via upload --- docs/Hashing/CollisionHandling.md | 72 +++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 docs/Hashing/CollisionHandling.md diff --git a/docs/Hashing/CollisionHandling.md b/docs/Hashing/CollisionHandling.md new file mode 100644 index 000000000..a90b78118 --- /dev/null +++ b/docs/Hashing/CollisionHandling.md @@ -0,0 +1,72 @@ +--- +id: collision-handling-in-hashing +sidebar_position: 5 +title: Collision Handling in Hashing +sidebar_label: Collision Handling in Hashing +tags: [hashing, data structures] +--- + +# Collision Handling in Hashing + +## Overview +In hashing, collision handling refers to the methods used to resolve the issue when two keys hash to the same index in a hash table. Collisions are inevitable in hash tables due to the limited number of available slots compared to the potentially infinite number of input keys. + +## Methods of Collision Handling + +### 1. Chaining (Open Hashing) +- Chaining stores multiple elements in the same bucket using a data structure like a linked list. +- When a collision occurs, the new key-value pair is added to the end of the linked list at that index. +- **Advantages**: Simple to implement, no need to resize the hash table. +- **Disadvantages**: Requires extra memory for pointers, performance degrades if many elements collide at the same index. + +### 2. Open Addressing (Closed Hashing) +- In open addressing, all elements are stored within the hash table itself, and when a collision occurs, alternative slots are probed to find an empty one. +- Common probing techniques include: + - **Linear Probing**: Increment the index sequentially until an empty slot is found. + - **Quadratic Probing**: Probe at quadratic intervals (i.e., 1, 4, 9, ...). + - **Double Hashing**: Use a second hash function to determine the probe step. +- **Advantages**: No need for extra memory for pointers, can be efficient for smaller tables. +- **Disadvantages**: Can lead to clustering (for linear probing), needs good probing strategies to avoid performance degradation. + +## Example: Chaining in Python + +```python +class HashTable: + def __init__(self): + self.table = [[] for _ in range(10)] # Hash table with 10 slots + + def insert(self, key, value): + index = hash(key) % len(self.table) + for pair in self.table[index]: + if pair[0] == key: + pair[1] = value # Update if key exists + return + self.table[index].append([key, value]) # Add new key-value pair + + def search(self, key): + index = hash(key) % len(self.table) + for pair in self.table[index]: + if pair[0] == key: + return pair[1] + return None + + def delete(self, key): + index = hash(key) % len(self.table) + for i, pair in enumerate(self.table[index]): + if pair[0] == key: + del self.table[index][i] + return + + + +# Example usage +hash_table = HashTable() +hash_table.insert('apple', 1) +hash_table.insert('banana', 2) +hash_table.insert('orange', 3) +print(hash_table.search('banana')) # Output: 2 +hash_table.delete('banana') +print(hash_table.search('banana')) # Output: None + +``` +