Number Of Slots In Hash Table

Number Of Slots In Hash Table 7,4/10 9001 votes

Though using an Array, we can search an element with time complexity O(1), but the array has its limitation such as it stores similar data types, each cell of array occupies the same amount of space and to find an element we require its index value. To find the Index value of an element itself can take a time complexity of O(n) or O(log n).

Using the concept of Hashing we can build a data structure that can search elements with constant time complexity.

The load factor is simply a measure of how full (occupied) the hash table is, and is simply defined as: α = number of occupied slots/total slots In simple words, consider we have a hash table of size 1000, and we have 500 slots filled, then the load factor would become α = 500/1000 = 0.5.

  1. New hash table is twice the size of the original hash table. To reduce the number of wasteful comparisons, it is important to avoid collisions of hashed key. This requires hash table with twice the number of slots as the number of expected elements. 4.1Linear Probe Hashing This is the most basic hashing scheme. It is also typically the fastest.
  2. For a hash table of size 100 or less, a reasonable distribution results. For a hash table of size 1000, the distribution is terrible because only slots 650 to 900 can possibly be the home slot for some key value, and the values are not evenly distributed even within those slots. Now you can try out this hash function.
  3. For this reason, chained hash tables remain effective even when the number of table entries (N) is much higher than the number of slots. For separate chaining, the worst-case scenario is when all the entries are inserted into the same linked list.

What is Hashing?

Number Of Slots In Hash Tableau

Hashing is a Technique in which we store data, in an array, at specific indices using some methods, rather than then storing it in ascending order, descending order or randomly. Suppose if we want to store 4 in an array, we perform some methods or operations on 4 and calculate the perfect index value for it, and if we want to retrieve 4 from the array, we just reverse that method or operation and get 4 with constant time complexity.

Hashing Table

Hashing Table or Hash Table is a collection of elements which are stored in a data structure using a Hashing method, which makes it easy to find them later. The Hash table consists of key and index or slot, here key represents the value which will store in the table and index or slot represent the index location of that key.

Each position of the hash table, slots, can hold an item and is named by an integer value starting at 0. We can use an Array to implement a hash table and initially all the elements of the array would be None.

For Example:

Insert 1, 3 ,5 , 7 , 8, 10, 11 in a hash table using hashing.

Create an array arbitrary size

Key-ValueHashing (key value % size of array)Array Index
11 % 7 =1
33 % 7 =3
55 % 7 =5
77 % 7 =0
99 % 7 =2
1010 % 7 =3 (collision) 3 is already occupied

Collision resolution = 3+1= 4

1111 % 7 =4 (Collison) = 4+1=5(collision)= 5+1 = 6

Array will be arr = [7, 1, 9, 3, 10, 5, 11, None, None, None, None, None, None]

Hashing Function

Hash function, are also known as Hashing methods and they are used to map each element or key to a unique slot or index. Hash is also used to minimize the number of collisions and using some easy methods it computes and evenly distributes the items in the hash table.

There are various hashing methods we can use to map a key to its slot:

  • Remainder Method
  • Folding Method
  • Mid Square Method

1. Reminder method

In the reminder method, we use the divide the key value with the total size of the table or array and use it remainder to specify the index or slot value of that key.

For example, if we want to insert 9 in an array of size 20, so it would be placed at 9%20 = 9th index if 9th index is free.

2. Folding Method

The folding method for constructing hash functions begins by dividing the item into equal-size pieces (the last piece may not be of equal size).

These pieces are then added together to give the resulting hash value.

SlotsNumberSlots

This hashing method applies to large digit numbers, for example, if we want a hash table in which key elements are the mobile numbers of the customers, this reminder method would not be efficient.

For instance:

If our key was the phone number 436-555-4601

We would take the digits and divide them into groups of 2 (43,65,55,46,01).

After the addition, 43+65+55+46+01, we get 210.

If we assume our hash table has 11 slots, then we need to perform the extra step of dividing by 11 and keeping the remainder.

210 % 11 is 1, so the phone number 436-555-4601 hashes to slot 1.

3. Mid Square Method

In the mid-square method, we to compute the key slot number or index location we first square the item and then extract some portion of the resulting digits.

Number Of Slots In Hash Table

For example:

Number Of Slots In Hash Table Search

if the item were 44, we would first compute 442=1,936.

By extracting the middle two digits, 93, and performing the remainder step, we get 93%11 = 5

Number Of Slots In Hash Tables

Implementation of Hash table

Number Of Slots In Hash Tablespoon

Python

Output: