HashTable:-
Hash table is faster than trees in terms of search and insertion (and sometimes deletion also).
The time complexity is constant for these operations O(1) , no matter how many data items are there.
Disadvantages of hash table:-
1) Hash Tables are based on array. Arrays are difficult to expand incase we do not have information on count of input.
2) Sorting the data in hash table is not very convinient.
Basically hash table is a table where each value has a key or we can say an id. This id can be treated as address for that value in a given data structure like array.
So if we are using an array for creating our hash table, then in that case the array index can be used as id/key. Now, we can use a function/method to calculate the key for a given value to be stored in the array acting as hash table.
Hashing:-
What prompted hashing:-
Though many scenarios prompted hashing, but one of the examples we can use is below:-
Want to create a dictionary of 50,000 words in computer memory. To do so, we can do the following:-
Approach 1 :- (Not a good approach)
Create an array of 50,000 length and store every word in each cell. But problem will be that how we will know which word lives in which index. So searching a word will become very slow.
Approach 2 :- (Not a good approach)
We know that we generally follow different flavors of character encoding standards like ASCII, UTF-8 etc. In these character encoding standards, we basically represent character with a number. Now with this approach we can sum all the numbers of the characters of a word and use that sum as index value which can be used to store the word. But that will create problems for palindrome words as they will have same index value when stored in array.
Approach 3 or Hashing :- (Good approach)
In hashing, we basically convert a large range into a number which is in a small range. The smaller range corresponds to index numbers an array. This function this operation of converting large range into small range is called hash function.
We can achieve this by doing following:-
To squeeze a large range into a number is small range, we can use modulo operator (%) which finds remainder when one number is divided by another. Lets understand this with an example:-
Large range :- 0 to 199
Small range :- 0 to 9
Now, size/length of small range is 10 (0,1,2,3,4,5,6,7,8,9)
Use below formula --> to find small number for a number from large range, say 157
small number = (large number) % (size/length of small range)
= 157 % 10
= 7
Now we can store word corresponding to 157 at index 7 in an array with an array size/limit of 10 (0 to 9)
Similarly we can index value in a small range array to store 50,000 dictionary words which is a large range.
So, in this case, the above formula small number = (large number) % (size/length of small range) can be called as hash function where we compute array index in the form of small number from the above formula.
An array where data is inserted using hash function is called a hash table.