{"id":1852,"date":"2019-01-02T12:00:00","date_gmt":"2019-01-02T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/introduction-to-hash-tables-and-direct-addressing\/"},"modified":"2026-07-05T03:20:29","modified_gmt":"2026-07-05T01:20:29","slug":"introduction-to-hash-tables-and-direct-addressing","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/introduction-to-hash-tables-and-direct-addressing\/","title":{"rendered":"Introduction to Hash Tables and Direct Addressing"},"content":{"rendered":"<p>We would cover the following:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li><a href=\"#t1\">Introduction to Hash Tables<\/a><\/li>\n<li><a href=\"#t2\">Arrays vs Hash Tables<\/a><\/li>\n<li><a href=\"#t3\">Direct-Address Tables<\/a><\/li>\n<li><a href=\"#t4\">Watch the Video on Hashing<\/a><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong id=\"t1\">1. Introduction to Hash Tables<\/strong><\/p>\n<p>A hash table is a data structure used for implementing dictionaries which support dictionary operations such as INSERT, SEARCH and DELETE. Such a data structure stored data in key-value pairs where each key is associated with a particular value.<\/p>\n<p>A typical application is in the compiler symbol table used during compilation. Here the keys corresponds to the identifiers (variables) used in the program\u00a0 which has corresponding values. In the worst case, searching for an element in a has table has the same performance as searching for an element in a\u00a0 linked list which is\u00a0\u0398(n). However, hash tables can achieve a constant time O(n) search performance on the average.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. Array vs Hash Tables<\/strong><\/p>\n<p>A hash table generalizes the notion of arrays where the elements can be accessed using the index of the particular element. This is exactly the case when the number of keys to store is the same as the number of positions available. In hash table however, there may be times when the total number of keys is more than the number of positions available in the table.<\/p>\n<p>So in the case of hash tables, unlike arrays, instead of using the key directly, the key is passed into a hash function which computes the index where the key is to be stored. The challenge with this is that sometimes, different keys passed to the same hash function may produce the same index (location). This is what is known as <strong>collision<\/strong>.\u00a0 As we would examine later, collision can be handles by <strong>chaining<\/strong>. Another way to deal with collisions is called open addressing.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t3\">3. Direct Address Tables<\/strong><\/p>\n<p>Direct addressing is a technique that works well in situation where the universe of possible keys is fairly small. Assuming an application requires a dynamic set in which each element to be stored has a key taken from the universe:<\/p>\n<p>U = {0, 1, . . . , m-1}<\/p>\n<p>where m is not too large.<\/p>\n<p>We also assume that no two elements would have the same key. To represent a dynamic set that meets these properties, w can use an array (which is also called direct-address table) represented as T[0. 1, . . . . m}.<\/p>\n<p>Here each position or slot in the table T, corresponds to a key in the universe U. This is shown in Figure 1.0<\/p>\n<figure id=\"attachment_358\" aria-describedby=\"caption-attachment-358\" style=\"width: 618px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-358 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Direct-Address-Table-1.jpg\" alt=\"\" width=\"618\" height=\"389\" \/><figcaption id=\"caption-attachment-358\" class=\"wp-caption-text\"><strong>Figure 1.0<\/strong>: Implementation of a dynamic set using the direct address table T. Each key in the universal set U = {0, 1, . . . 8} correspond to an index in the direct address table. The set K = {2, 3, 5, 8} is the set of actual keys stored in the table and determines the slots of positions in the table that contains pointer to the data. The other slots are empty (contains NIL)<\/figcaption><\/figure>\n<p>In Figure 1.0, we notice that the direct address table stores elements in external object (represent in light green key-data pair), with a pointer to the object. However, in some application, the elements can actually be stored in the table itself instead of on an external object.<\/p>\n<p>Direct-address table operation is given below all of which takes O(1) time.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">SEARCH<\/span> (<span style=\"color: #333399; font-weight: bold;\">T<\/span>, k)\n\treturn <span style=\"color: #333399; font-weight: bold;\">T<\/span>[k]\n\n<span style=\"color: #333399; font-weight: bold;\">INSERT<\/span> (<span style=\"color: #333399; font-weight: bold;\">T<\/span>, x)\n\t<span style=\"color: #333399; font-weight: bold;\">T<\/span>[x<span style=\"color: #333333;\">.<\/span>key] <span style=\"color: #000000; font-weight: bold;\">=<\/span> x\n\n<span style=\"color: #333399; font-weight: bold;\">DELETE<\/span> (<span style=\"color: #333399; font-weight: bold;\">T<\/span>, x)\n\t<span style=\"color: #333399; font-weight: bold;\">T<\/span>[x<span style=\"color: #333333;\">.<\/span>key] <span style=\"color: #000000; font-weight: bold;\">=<\/span> <span style=\"color: #333399; font-weight: bold;\">NIL<\/span>\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">Watch the Video on Hashing<\/strong><br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/2bAeF2NhO-g\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/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>We would cover the following: Introduction to Hash Tables Arrays vs Hash Tables Direct-Address Tables Watch the Video on Hashing &nbsp; 1. Introduction to Hash &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-1852","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1852","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=1852"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1852\/revisions"}],"predecessor-version":[{"id":2021,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1852\/revisions\/2021"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}