2. Hash Table
• Hash Table is a data structure which stores data in an associative
manner. In a hash table, data is stored in an array format, where each
data value has its own unique index value. Access of data becomes
very fast if we know the index of the desired data.
• Thus, it becomes a data structure in which insertion and search
operations are very fast irrespective of the size of the data. Hash
Table uses an array as a storage medium and uses hash technique to
generate an index where an element is to be inserted or is to be
located from.
3.
4. Linear vs binary search
• In linear search we have a list in random order
• But is binary search the array is in sorted form. But we need to sort it
first.
• This consume extra time.
• But somehow its better time complexity which is O(logn).
• Now we want a time complexity O(1).
• O(n) O(logn) O(1)
• So this is the requirement which introduces hashing method.
17. Hashing
• We map the
elements
according to
the function.
• 8%10 = 8
• 3%10 = 3
• 13%10 = 3
• Here is a
collision.
18. Hashing
• There is one hitch: two keys may hash to the same slot. We call this
situation a collision. Fortunately, we have effective techniques for
resolving the conflict created by collisions. Of course, the ideal solution
would be to avoid collisions altogether. We might try to achieve this
goal by choosing a suitable hash function h. One idea is to
make h appear to be “random,” thus avoiding collisions or at least
minimizing their number. The very term “to hash,” evoking images of
random mixing and chopping, captures the spirit of this approach.
20. Resolution of collision
• Chaining
• We add a node/ linked
list to store the
element at the same
location in chains.
21. Resolution of collision
• Chaining
• We add a node/ linked
list to store the
element at the same
location in chains.
• If we search a 13?
22. Resolution of collision
• Chaining
• We add a node/ linked
list to store the element
at the same location in
chains.
• If we search a 13?
• Can be placed at 3rd
location. But if we
search for 13 then it
could take a more the
O(1) time but its better
then O(logn).
23. Resolution of collision
• Linear Probing
• Place 3 at location 3
and for 13 (13%10 = 3)
, we see the next
empty location and
put at location 4.
24. Resolution of collision
• Linear Probing
• Place 3 at
location 3 and
for 13 (13%10 =
3), here is a
collision. so we
see the next
empty location
and put at
location 4.
25. Resolution of collision
• Linear Probing
• Place 3 at
location 3 and
for 13 (13%10 =
3), here is a
collision. so we
see the next
empty location
and put at
location 4.
• Now if we want
to store 4 here is
a collision.
26. Resolution of collision
• Linear Probing
• If we search for
a 4, go to the
index 4 and
look in it. Not
found then go
to the next
element.
27. Resolution of collision
• Linear Probing
• Searching for 24.
• 24%10 = 4
• Go to location 4, its not
available, then move on
until the empty
location.
• Here we also get the
complexity more then
O(1).
• But its better then
O(logn).
28. Resolution of collision
• Linear Probing
• Problem in linear
probing is bunch of
elements at the
same place. So it
cause a problem in
searching.
• Here we also get
the complexity
more then O(1).
• But its better then
O(logn).
29. Resolution of collision
• Quadratic probing
• To avoid the
clustering of
elements we
introduced this
method.
30. Resolution of collision
• Quadratic probing
• Only difference is i
= i^2
• Which give us a
location far away.
• For 23 it could be 7.
• This will avoid the
clustering and
improve the
searching time.