1. Gyanastra
  2. Data Structures
  3. Hashing
  4. Direct Address Tables
Free lesson, no account needed 4 min read 3 practice questions after it
Asked in past papers3 times
  • GATE CSE1992
  • ISRO2017
  • UGC NET2014

Data Structures · Hashing

Direct Address Tables

Overview

A direct address table is an array in which each record is stored at the index equal to its unique key. It is the technique that precedes hashing and the first data structure to achieve searching in constant time, O(1)O(1).

Key concepts

  • Direct address table: A data structure that works just like an array; the key itself is used as the array index.
  • Unique key: Every record to be stored has its own distinct key, so no two records compete for the same slot.
  • Small known key range: The keys lie in a small, known range (for example 1 to 100), so one slot can be provided per possible key.
  • Random access: An array is a random access data structure, so any index is reached directly in one step; since the key is the index, no comparisons are needed.
  • Proportionality condition: The table is useful only when there is a direct proportionality between the number of keys used and the size of the array.

How it works

  1. Define an array whose size is at least as large as the largest key value.
  2. Give every possible key its own slot in the array — one slot per key.
  3. Insert: go directly to the index equal to the element's key and place the element there.
  4. Search: go directly to the index equal to the key being searched and look there.
  5. Because access is direct, both insert and search take O(1)O(1) time.

Formulas

  • index of an element=its key value\text{index of an element} = \text{its key value} (key kk is stored at array position kk)
  • search time=O(1)\text{search time} = O(1) (direct index access, no comparisons)
  • array size≥max⁡(key)\text{array size} \ge \max(\text{key})

Worked examples

Example 1 — keys in a small range (works well)

  • 100 records with unique keys in the range 1 to 100.
  • Declare an array of size 100.
  • Insert the element with key 10 → store it at index 10.
  • Search for key 10 → access index 10 directly → found in O(1)O(1) time.

Example 2 — few keys but very large key values (fails)

  • Only 10 elements are to be inserted, but the key values are very large — one key is 1 million.
  • Placing a key of value 1 million requires an index as large as that key.
  • The array size may therefore have to be 1 million just to store 10 values.
  • Conclusion: the direct address table fails here because it wastes space.

Comparison

SituationDirect address table behaviour
Key size and number of elements close to each other (both in the hundreds)Useful — one slot per element is reasonable
Key size very large, number of keys inserted very smallWaste of space — fails; hashing is used instead

Revision summary

  • The direct address table is an array where the key is the index; this is what gives O(1)O(1) search.
  • It is the first data structure that supported searching in O(1)O(1) time.
  • It works only when keys lie in a small range and the number of keys is directly proportional to the array size.
  • The array must be at least as large as the largest key value.
  • Trap: few elements with very large keys (e.g. 10 elements, one key 1 million) force an array of size 1 million — a waste of space.
  • When key size is very large and the number of inserted keys is very small, hashing is used instead of a direct address table.

Previous-year exam insights

  • Direct addressing gives O(1)O(1) worst-case search/insert/delete; hashing gives O(1)O(1) average-case but O(n)O(n) worst-case (all keys colliding).
    Asked inUGC NET 2014
  • Direct addressing requires the key universe to be small and known in advance; when keys are sparse or very large, hashing (chaining or open addressing) is preferred.
    Asked inISRO 2017
  • A bit-map (bit-vector) index uses one bit per distinct value of a low-cardinality attribute and is efficient for queries with equality conditions on such attributes.
    Asked inISRO 2017
  • In a direct address table, delete is also O(1)O(1): set the slot to a null/sentinel marker; no collision handling or probing is needed.
  • Space complexity of a direct address table is Θ(U)\Theta(U) where U is the size of the key universe, independent of the number n of keys stored.
  • Hashing cannot support ordered operations (min, max, successor, range queries) efficiently, whereas balanced trees/B+ trees can; choose the index accordingly.
    Asked inISRO 2017
Pages 1–2 of 2