Dsa Hashingppt

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

The Hash Table organizations

• Hashing is one of the searching techniques that uses a constant time.


The time complexity in hashing is O(1). Till now, we read the two
techniques for searching, i.e., linear search and binary search. The
worst time complexity in linear search is O(n), and O(logn) in binary
search.

• In both the searching techniques, the searching depends upon the


number of elements but we want the technique that takes a constant
time. So, hashing technique came that provides a constant time.

• In Hashing technique, the hash table and hash function are used. Using
the hash function, we can calculate the address at which the value can
be stored.
• The main idea behind the hashing is to create the (key/value) pairs. If the
key is given, then the algorithm computes the index at which the value
would be stored. It can be written as:
Direct Address Table
• Direct Address Table is a data structure that has the capability of mapping
records to their corresponding keys using arrays. In direct address tables,
records are placed using their key values directly as indexes. They
facilitate fast searching, insertion and deletion operations.

• If we have a collection of n elements whose key are unique integers in


(1,m), where m >= n, then we can store the items in a direct address table
shown in figure, T[m], where Ti is either empty or contains one of the
elements of our collection.

• Searching a direct address table is clearly an O (1) operation: for a key, k,


we access Tk,
1) If it contains an element, return it.
2) If it doesn't then return a NULL.
There are two constraints here:
1.The keys must be unique.
2. The range of the key must be severely bounded.
• If the keys are not unique, then we can simply construct a set of m lists
and store the heads of these lists in the direct address table. The time to
find an element matching an input key will still be O (1).

• However, if each element of the collection has some other


distinguishing feature (other than its key), and if the maximum number
of duplicates is ndupmax, then searching for a specific element is
O(ndupmax). If duplicates are the exception rather than the rule, then
ndupmax is much smaller than n and a direct address table will provide
good performance. But if ndupmax approaches n, then the time to find
a specific element is O(n) and a tree structure will be more efficient.

• The range of the key determines the size of the direct address table and
may be too large to be practical. For instance, it's not likely that you'll
be able to use a direct address table to store elements which have
arbitrary 32- bit integers as their keys for
a few years yet!
• Direct addressing is easily generalized to the case where there is a
function,

h(k) => (1, m)


• which maps each value of the key, k, to the range (1, m). In this case,
we place the element in T[h(k)] rather than T[k] and we can search in
O (1) time as before.

Advantages:

• Searching in O(1) Time: Direct address tables use arrays which are


random access data structure, so, the key values (which are also the
index of the array) can be easily used to search the records in O(1)
time.

• Insertion in O(1) Time: We can easily insert an element in an array in


O(1) time. The same thing follows in a direct address table also.
Deletion in O(1) Time: Deletion of an element takes O(1) time in an
array. Similarly, to delete an element in a direct address table we need
O(1) time.

Limitations:

• Prior knowledge of maximum key value

• Practically useful only if the maximum value is very less.

• It causes wastage of memory space if there is a significant difference


between total records and maximum value.
THANK YOU

You might also like