# Folding Method in Hashing

• Difficulty Level : Basic
• Last Updated : 04 Jul, 2021

Folding Method in Hashing: It breaks up a key value into precise segments that are added to form a hash value, and look at another technique is to apply a multiplicative hash function to each segment individually before adding. Some folding methods go one step further and reverse every other piece before the addition. This folding method is independent of distribution.

Algorithm:

• The folding method is used for creating hash functions starts with the item being divided into equal-sized pieces i.e., the last piece may not be of equal size.
• The outcome of adding these bits together is the hash value, H(x) = (a + b + c) mod M, where a, b, and c represent the preconditioned key broken down into three parts and M is the table size, and mod stands for modulo.
• In other words, the sum of three parts of the preconditioned key is divided by the table size. The remainder is the hash key.

Explanation:

Example 1: The task is to fold the key 123456789 into a Hash Table of ten spaces (0 through 9).

• It is given that the key, say X is 123456789 and the table size (i.e., M = 10).
• Since it can break X into three parts in any order. Let’s divide it evenly.
• Therefore, a = 123, b = 456, c = 789.
• Now, H(x) = (a + b + c) mod M i.e., H(123456789) =(123 + 456 + 789) mod 10 = 1368 mod 10 = 8.
• Hence, 123456789 is inserted into the table at address 8.

Example 2: The task is to fold the key 452378912 into a Hash Table of ten spaces (0 through 9).

• It is given that the key, say X is 452378912 and the table size (i.e., M = 10).
• Since it can break X into three parts in any order. Let’s divide it evenly.
• Therefore, a = 452, b = 378, c = 912.
• Now, H(x) = (a + b + c) mod M i.e., H(452378912) =(452 + 378 + 912) mod 10 = 1742 mod 10 = 2.
• Hence, 452378912 is inserted into the table at address 2.
My Personal Notes arrow_drop_up
Related Articles
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://origin.geeksforgeeks.org/folding-method-in-hashing/", "fetch_time": 1669744323000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2022-49/segments/1669446710710.91/warc/CC-MAIN-20221129164449-20221129194449-00543.warc.gz", "warc_record_offset": 467452284, "warc_record_length": 27117, "token_count": 543, "char_count": 1954, "score": 3.625, "int_score": 4, "crawl": "CC-MAIN-2022-49", "snapshot_type": "latest", "language": "en", "language_score": 0.8787885308265686, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00030-of-00064.parquet"}