From 58367d3c7cc813d4311195c8e74cd28f69cdaa4f Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Thu, 26 Oct 2023 17:48:45 +0530 Subject: [PATCH 1/3] Added doctest & docstring to quadratic_probing.py --- data_structures/hashing/quadratic_probing.py | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/data_structures/hashing/quadratic_probing.py b/data_structures/hashing/quadratic_probing.py index 0930340a347f..209e16cb1634 100644 --- a/data_structures/hashing/quadratic_probing.py +++ b/data_structures/hashing/quadratic_probing.py @@ -12,6 +12,40 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def _collision_resolution(self, key, data=None): + """ + Quadratic probing is an open addressing scheme used for resolving + collisions in hash table. + + It works by taking the original hash index and adding successive + values of an arbitrary quadratic polynomial until open slot is found. + + Hash + 1², Hash + 2², Hash + 3² .... Hash + n² + + e.g: + 1. Create hash table with size 7 + >>> qp = QuadraticProbing(7) + >>> qp.insert_data(90) + >>> qp.insert_data(340) + >>> qp.insert_data(24) + >>> qp.insert_data(45) + >>> qp.insert_data(99) + >>> qp.insert_data(73) + >>> qp.insert_data(7) + >>> qp.keys() + {11: 45, 14: 99, 7: 24, 0: 340, 5: 73, 6: 90, 8: 7} + + 2. Create hash table with size 8 + >>> qp = QuadraticProbing(8) + >>> qp.insert_data(0) + >>> qp.insert_data(999) + >>> qp.insert_data(111) + >>> qp.keys() + {0: 0, 7: 999, 3: 111} + + reference: + - https://en.wikipedia.org/wiki/Quadratic_probing + """ + i = 1 new_key = self.hash_function(key + i * i) @@ -27,3 +61,9 @@ def _collision_resolution(self, key, data=None): break return new_key + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 5d6ccec967ee39ba57a10fb337f4bf74eb1e7188 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Thu, 26 Oct 2023 19:51:42 +0530 Subject: [PATCH 2/3] Update quadratic_probing.py --- data_structures/hashing/quadratic_probing.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/data_structures/hashing/quadratic_probing.py b/data_structures/hashing/quadratic_probing.py index 209e16cb1634..cacc7e500493 100644 --- a/data_structures/hashing/quadratic_probing.py +++ b/data_structures/hashing/quadratic_probing.py @@ -21,6 +21,8 @@ def _collision_resolution(self, key, data=None): Hash + 1², Hash + 2², Hash + 3² .... Hash + n² + reference: + - https://en.wikipedia.org/wiki/Quadratic_probing e.g: 1. Create hash table with size 7 >>> qp = QuadraticProbing(7) @@ -41,9 +43,6 @@ def _collision_resolution(self, key, data=None): >>> qp.insert_data(111) >>> qp.keys() {0: 0, 7: 999, 3: 111} - - reference: - - https://en.wikipedia.org/wiki/Quadratic_probing """ i = 1 From 1ac215b35d72f9ad87268ee5754d75cd4d2c04de Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:31:52 +0530 Subject: [PATCH 3/3] Update quadratic_probing.py --- data_structures/hashing/quadratic_probing.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/data_structures/hashing/quadratic_probing.py b/data_structures/hashing/quadratic_probing.py index cacc7e500493..2f3401ec8918 100644 --- a/data_structures/hashing/quadratic_probing.py +++ b/data_structures/hashing/quadratic_probing.py @@ -43,6 +43,22 @@ def _collision_resolution(self, key, data=None): >>> qp.insert_data(111) >>> qp.keys() {0: 0, 7: 999, 3: 111} + + 3. Try to add three data elements when the size is two + >>> qp = QuadraticProbing(2) + >>> qp.insert_data(0) + >>> qp.insert_data(999) + >>> qp.insert_data(111) + >>> qp.keys() + {0: 0, 4: 999, 1: 111} + + 4. Try to add three data elements when the size is one + >>> qp = QuadraticProbing(1) + >>> qp.insert_data(0) + >>> qp.insert_data(999) + >>> qp.insert_data(111) + >>> qp.keys() + {4: 999, 1: 111} """ i = 1