{"id":1851,"date":"2019-01-03T12:00:00","date_gmt":"2019-01-03T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/hashing\/"},"modified":"2026-07-05T03:20:26","modified_gmt":"2026-07-05T01:20:26","slug":"hashing","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/hashing\/","title":{"rendered":"Linear Probing, Quadratic Probing and Double Hashing"},"content":{"rendered":"<p>The three terms that make up the title of this article are the three common techniques used for computing hash sequences. That is what are are going to cover today<\/p>\n<ol>\n<li><a href=\"#t1\">Linear Probing<\/a><\/li>\n<li><a href=\"#t2\">Quadratic Probing<\/a><\/li>\n<li><a href=\"#t3\">Double Hashing<\/a><\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-380\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Linear-Probing-Quadratic-Probing-and-Double-Hashing-300x217.jpg\" alt=\"Introduction to Hashing\" width=\"206\" height=\"149\" \/><\/p>\n<h5><strong id=\"t1\">1. Linear Probing<\/strong><\/h5>\n<p>We start with a normal has function h that maps the universe of keys U into slots in the hash table T such that<\/p>\n<p>h&#8217; : U\u00a0\u2192 {0, 1, 2, . . . , m-1}<\/p>\n<p>h&#8217; is a normal hash function which we would call the auxiliary hash function.\u00a0 Now if we use linear probing, we would have a hash function like this:<\/p>\n<p>h(k, i) = (h'(k) + i ) mod m<\/p>\n<p>for m = 0, 1, 2, . . .,m-1<\/p>\n<p>Given a particular key k, the first step is to examine T[h'(k)]\u00a0 which is the\u00a0 slot given by the auxiliary hash function. Next, we examine slot T[h'(k) +1], then we examine T[h'(k) +2] and so on up to the last slot which is T[m-1]. The wrap around back to the beginning of the table to position T[0], T[1] and so on until T[h'(k) -1].\u00a0 This is presented in the table below:<\/p>\n<ul>\n<li>T[h'(k)]<\/li>\n<li>T[h'(k) + 1]<\/li>\n<li>T[h'(k) + 2]<\/li>\n<li>&#8230;<\/li>\n<li>&#8230;<\/li>\n<li>T[m-1]<\/li>\n<li>T[0]<\/li>\n<li>T[1]<\/li>\n<li>&#8230;<\/li>\n<li>&#8230;<\/li>\n<li>T[h'(k) &#8211; 1]<\/li>\n<\/ul>\n<p>It is therefore guaranteed that there will be m distinct probe sequences since the initial probe determines the whole probe sequence.<\/p>\n<p>Linear Probing have the advantage of being easy to implement but has one draw back. And that is a problem known as primary clustering. This is a situation where long runs of positions build up. This in turn leads to increased average search time.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Quadratic Probing<\/strong><\/h5>\n<p>Quadratic Probing is similar to linear probing but in quadratic probing the hash function used is of the form:<\/p>\n<p>h(k, i) = (h'(k) +\u00a0 c<sub>1<\/sub>i\u00a0 + c<sub>2<\/sub>i<sup>2<\/sup>) mod m<\/p>\n<p>where h&#8217; is the auxiliary hash function and c<sub>1<\/sub> and c<sub>2<\/sub> are called positive auxiliary constants.<\/p>\n<p>i = 0, 1, 2, . . . , m-1<\/p>\n<p>Just as with linear probing, the initial probe position is T[h'(k)]; Subsequent positions probed are not linear but are offset by\u00a0 an amount that depends on the quadratic nature of the probe number i.<\/p>\n<p>The method of quadratic probing is found to be better than linear probing. However, to ensure that the full hash table is covered, the values of c<sub>1<\/sub>, and c<sub>2<\/sub> are constrained. It may happen that two keys produce the same probe sequence such that:<\/p>\n<p>h(k<sub>1<\/sub>, i) = h(k<sub>2<\/sub>, i)<\/p>\n<p>Therefore, this leads to a kind of clustering called secondary clustering. Just as in linear probing, the initial probe position determines the entire probe sequence.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Double Hashing<\/strong><\/h5>\n<p>Double Hashing is considered to be the best method of hashing for open addressing compared to linear and quadratic probing. In this case, two auxiliary functions h<sub>1<\/sub> and h<sub>2<\/sub> are used.\u00a0 Hash function for double hashing take the form:<\/p>\n<p>h(k, i) = (h<sub>1<\/sub>(k) + ih<sub>2<\/sub>(k)) mod m<\/p>\n<p>h<sub>1<\/sub> and h<sub>2<\/sub> are the auxiliary functions.<\/p>\n<p>Just like before, the initial probe position is T[h<sub>1<\/sub>(k)]. Subsequent hash positions are offset from previous position by the amount h<sub>2<\/sub>(k) modulo m. So we can see that the probe sequence depends on the key in two ways. This is because the initial probe position, the offset or both may vary.<\/p>\n<p>However, value of h<sub>2<\/sub>(k) need to be relatively prime. for the whole hash table to be searched. One way to achieve this is to let m be a power of 2 and then to design h<sub>2<\/sub> such that it will always produce and odd number. Another ways is to let me be a prime number and the to design h<sub>2<\/sub> to always returns a positive integer that is less than m.<\/p>\n<p>Take and example<\/p>\n<p>h<sub>1<\/sub>(k) = k mod m<\/p>\n<p>h<sub>2<\/sub>(k) = 1 + (k mod m&#8217;)<\/p>\n<p>where m&#8217; is a little less than m.<\/p>\n<p>Finally, we would not take some time to do and Analysis of Open Addressing with Linear Probing in the next lesson.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong>Check these Data Structure and Algorithms Tutorials<\/strong><\/h5>\n<ul>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/skip-lists-and-how-they-work\/\">Skip Lists and How They Work<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/perfect-hashing\/\">Perfect Hashing \u2013 How it Works<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hashing\/\">Linear Probing, Quadratic Probing and Double Hashing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hashing-with-open-addressing\/\">Hashing With Open Addressing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/universal-hashing\/\">Universal Hashing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/search-time-under-simple-uniform-hashing\/\">Search Time Under Simple Uniform Hashing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hash-tables-hashing-with-chaining\/\">Hash Tables \u2013 Hashing With Chaining<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/introduction-to-hash-tables-and-direct-addressing\/\">Introduction to Hash Tables and Direct Addressing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/recurrences-in-divide-and-conquer-algorithm\/\">Recurrences in Divide and Conquer Algorithm<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/how-bloom-filters-work\/\">How Bloom Filters Work<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/cuckoo-hashing\/\">How Cuckoo Hashing Works<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/introduction-to-avl-trees-with-examples\/\">Introduction to AVL Trees (With Examples)<\/a><\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The three terms that make up the title of this article are the three common techniques used for computing hash sequences. That is what are &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[35],"tags":[],"class_list":["post-1851","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1851"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1851\/revisions"}],"predecessor-version":[{"id":2020,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1851\/revisions\/2020"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}