Unit-1-1
Unit-1-1
Searching
What is Search?
• Search is a process of finding a value in a list of values. In other words,
searching is the process of locating given value position in a list of values.
Linear Search
Algorithm
(Sequential Search)
Searching
Linear search algorithm finds given element in a list of elements
with O(n) time complexity where n is total number of elements in the list.
This search process starts comparing of search element with the first element
in the list.
If both are matching then results with element found otherwise search
element is compared with next element in the list.
Otherwise, repeat the same with the next element in the list until search
element is compared with last element in the list, if that last element also
doesn't match, then the result is "Element not found in the list".
That means, the search element is compared with element by element in the
list.
Searching
Algorithm
Step 1: Read the search element from the user
Step 2: Compare, the search element with the first element in
the list.
Step 3: If both are matching, then display "Given element
found!!!" and terminate the function
Step 4: If both are not matching, then compare search element
with the next element in the list.
Step 5: Repeat steps 3 and 4 until the search element is
compared with the last element in the list.
Step 6: If the last element in the list is also doesn't match, then
display "Element not found!!!" and terminate the function.
Searching
Example
Searching
Searching
Searching
Binary Search
Algorithm
Binary Search
Binary search algorithm finds given element in a list of elements with O(log
n) time complexity where n is total number of elements in the list.
The binary search algorithm can be used with only sorted list of element.
That means, binary search can be used only with list of element which are already
arranged in a order.
The binary search can not be used for list of element which are in random order.
This search process starts comparing of the search element with the middle
element in the list.
If both are matched, then the result is "element found". Otherwise, we check
whether the search element is smaller or larger than the middle element in the
list.
If the search element is smaller, then we repeat the same process for left sublist
of the middle element.
If the search element is larger, then we repeat the same process for right sublist of
the middle element.
We repeat this process until we find the search element in the list or until we left
with a sublist of only one element.
And if that element also doesn't match with the search element, then the result is
"Element not found in the list".
Binary Search
Algorithm
Step 1: Read the search element from the user
Step 2: Find the middle element in the sorted list
Step 3: Compare, the search element with the middle element in the
sorted list.
Step 4: If both are matching, then display "Given element found!!!" and
terminate the function
Step 5: If both are not matching, then check whether the search
element is smaller or larger than middle element.
Step 6: If the search element is smaller than middle element, then
repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.
Step 7: If the search element is larger than middle element, then
repeat steps 2, 3, 4 and 5 for the right sublist of the middle element.
Step 8: Repeat the same process until we find the search element in
the list or until sublist contains only one element.
Step 9: If that element also doesn't match with the search element,
then display "Element not found in the list!!!" and terminate the
function.
Binary Search
Example
Binary Search
Binary Search
HASH TABLES
In Data Structures,
There are several searching techniques like linear search, binary search, search trees etc.
In these techniques, time taken to search any particular element depends on the total number of
elements.
Example-
Linear Search takes O(n) time to perform the search in unsorted arrays consisting of n elements.
Binary Search takes O(logn) time to perform the search in sorted arrays consisting of n elements.
It takes O(logn) time to perform the search in Binary Search Tree consisting of n elements.
Drawback
The main drawback of these techniques is
As the number of elements increases, time taken to perform the search also increases.
This becomes problematic when total number of elements become too large.
HASHING
Hashing is a well-known technique to search any particular element among several elements.
It minimizes the number of comparisons while performing the search.
ADVANTAGE
In hashing,
An array data structure called as Hash table is used to store the data items.
Based on the hash key value, data items are inserted into the hash table.
TYPES OF HASHING
DYNAMIC HASHING
In this hashing table, the hash function is modified dynamically number of records grow.
HASH FUNCTION
Hash function is a function that maps any big number or string to a small integer value.
Hash function takes the data item as an input and returns a small integer value as an
output.
The small integer value is called as a hash value.
Hash value of the data item is then used as an index for storing it into the hash table.
TYPES OF HASH FUNCTIONS
In the mid square method, the key is squared and the middle or mid part of the result is used as
index or position or location.
Example the records 311, 3112, 3114 are inserted to hash table. Assume that hash table size is
1000.
H (Key) = (3112)2
3111 783
= 9684544
845 is the middle part of 9684544. So, 845 is the index of 3112.
3112 845
= 9690769
999
907 is the middle part of 9690769. So, 907 is the index of 3113.
MULTIPLICATIVE HASH FUNCTION
The given record is multiplied by some constant value. The formula computing hash key is
H (Key) = floor (P*(fractional part of key*A))
Where ‘P’ is an integer constant and ‘A’ is real constant.
Donald Knuth suggested to use constant A = 0.61803398987.
EXAMPLE:
Insert the following records 107, 108, 109, 110 into hash table . Here P =50.
107 inserted into hash table by using multiplicative hash function.
H (Key) = floor (P*(fractional part of key*A))
= floor (50*(107* 0.61803398987)
= floor (3306.4818)
=3306
108 inserted into hash table by using multiplicative hash function. 0
H (Key) = floor (50*(108* 0.61803398987)
= floor (3337.3835) 107 3306
= 3337
109 inserted into hash table by using multiplicative hash function.
108 3337
H (Key) = floor (50*(109* 0.61803398987)
= floor (3368.2852)
109 3368
= 3368
110 inserted into hash table by using multiplicative hash function.
H (Key) = floor (50*(110* 0.61803398987) 110 3399
The key value is divided into separate parts and using some simple operation this parts are
combined to produce hash key.
EXAMPLE:
Consider the record 1, 2, 3, 6, 5, 4, 1, 2 then it is divided into separate parts 123, 654, 12 and
this all are added together.
The record 123, 654, 12 will be placed at a location 789 in the hash table.
COLLISION RESOLUTION TECHNIQUE
Hashing is a well-known searching technique.
It minimizes the number of comparisons while performing the search.
It completes the search with constant time complexity O(1).
COLLISION
When the hash value of a key maps to an already occupied bucket of the hash table,
it is called as a Collision.
If collision occurs then it should be handled by applying some techniques. Such techniques are called collision
resolution technique.
The goal of collision resolution techniques is to minimize collisions. There are two methods of handling
collisions.
COLLISION RESOLUTION TECHNIQUES
Collision Resolution Techniques are the techniques used for resolving or handling the collision
If Load factor (α) = constant, then time complexity of Insert, Search, Delete = Θ(1 )
PRACTICE PROBLEM BASED ON SEPARATE CHAINING
Using the hash function ‘key mod 7’, insert the following sequence of keys in the hash table.
SOLUTION
The given sequence of keys will be inserted in the hash table as-
STEP-01:
STEP-03:
The next key to be inserted in the hash table = 700.
Bucket of the hash table to which key 700 maps = 700 mod 7 = 0.
So, key 700 will be inserted in bucket-0 of the hash table as.
STEP-04:
The next key to be inserted in the hash table = 76.
Bucket of the hash table to which key 76 maps = 76 mod 7 = 6.
So, key 76 will be inserted in bucket-6 of the hash table as.
STEP-05:
The next key to be inserted in the hash table = 85.
Bucket of the hash table to which key 85 maps = 85 mod 7 = 1.
Since bucket-1 is already occupied, so collision occurs.
Separate chaining handles the collision by creating a linked list to bucket-1.
So, key 85 will be inserted in bucket-1 of the hash table as.
STEP-06:
IN CASE OF COLLISION,
Probing is performed until an empty bucket is found.
Once an empty bucket is found, the key is inserted.
Probing is performed in accordance with the technique used for open addressing.
SEARCH OPERATION
To search any particular key,
Its hash value is obtained using the hash function used.
Using the hash value, that bucket of the hash table is checked.
If the required key is found, the key is searched.
Otherwise, the subsequent buckets are checked until the required key or an
empty bucket is found.
The empty bucket indicates that the key is not present in the hash table.
DELETE OPERATION
The key is first searched and then deleted.
After deleting the key, that particular bucket is marked as “deleted”.
OPEN ADDRESSING TECHNIQUES
1. Linear Probing
ADVANTAGE
It is easy to compute.
DISADVANTAGE
The main problem with linear probing is clustering.
Many consecutive elements form groups.
Then, it takes time to search an element or to find an empty bucket.
TIME COMPLEXITY
This is because
Even if there is only one element present and all other elements are deleted.
Then, “deleted” markers present in the hash table makes search the entire table.
EXAMPLE:
Consider that following keys are to be inserted in the hash table 131, 4, 8,
7, 21, 5, 31, 61, 9, 29.The hash table size is 10. 0 Null
1 131
Initially we will put the following keys in the hash table 131, 4, 8, 7. 2 Null
We will use division hash function. That means that keys are placed using 3 Null
4 4
formula.
5 Null
H (Key) = key % table size 6 Null
7 7
For instance the element 131 can be placed at H (Key) = 131 % 10 =1. 8 8
9 Null
Index 1 will be the home bucket for 131. Continuing in the fashion we will
place 4,8,7.
Now the next to be inserted is 21. According to hash function 0 Null
H (Key) = 21 % 10 =1.
1 131
2 21
But the index 1 location already occupied with 131 i.e., collision
3 Null
occurs. To resolve this collision we will linearly move down from 1
4 4
to empty location is found.
5 5
Therefore 21 will be placed at index 2.
6 Null
If the next element is 5 then we get home bucket for 5 as index 5 this
7 7
bucket is empty so, we will put the element 5 at index 5.
8 8
9 Null
After placing record keys 31, The next record key that comes is 9. According to decision as
61 the hash table will be function it demands for the home bucket 9. Hence we will
place 9 at index 9.
0 Null
1 131 0 Null 0 29
2 21 1 131 1 131
3 31 2 21 2 21
4 4 3 31 3 31
5 5 4 4 4 4
6 61 5 5 5 5
7 7 6 61 6 61
8 8 7 7 7 7
9 Null 8 8 8 8
9 9 9 9
Now the next final record key is 29 and it hashes a key 9. But home bucket 9 is already
occupied. And there is no next empty bucket as the table size is limited to index 9. The
overflow occurs to handle it we move back to bucket 0 and is the location over there is empty
29 will be placed at 0th index.
QUADRATIC PROBING
Quadratic probing operates by taking original hash value and adding successive values of quadratic
polynomial to the stating value.
0 49
1 87
2 22
3
4
5 55
6
7 37
8 17
9 19
DOUBLE PROBING
OR
DOUBLE HASHING
Double hashing is a technique in which a second hash function is applied to key when a collision
occur by applying the second has function we will get number of positions from the point of Collision
inserted. 0 90
1
By using following formulas we can find out the double hashing. 37 % 10 = 7
2 22
90 % 10 = 0 3
H1 (key) = k % table size
45 % 10 = 5 4
H2(key) = M - (K % M) 22 % 10 = 2 5 45
6
Where M is prime number smaller than the size of the table. 7 37
8
Example: consider the following elements to be placed in the Hash table of size 10. 9
Inside Initially the elements using the formula for H1 (key). Insert 37, 90, 45, 22.
NOW IF 17 IS TO BE INSERTED THEN
H1 (17) = 17 % 10 = 7
Here collision will be occur because 7th position already occupied with element 37 or record 37.
So we can apply second hash function to key.
0 90
H2 (key) = M-(K%M) 1 17
Here M is prime number smaller than the size of the table. 2 22
3
Let us prime number is M = 7
4
H2 (17) =7-(17%7) 5 45
= 7 - 3 = 4 ( 4 jumps from the collision index) 6
7 37
17 will be placed at index 1.
8
9
Now to insert number 49 at location 9th position that is 49 % 10 = 9.
0 90
1 17 Now to insert number 55.
2 22 H1 (55) = 55 % 10 = 5 that is collision will be occur. Because the location 5 already
3 occupied with 45. So, we can apply second hash function.
4
H2 (55) = 7 - (55 % 7) = 7 - 6 = 1 ( 1 jumps from the collision index)
5 45
6 That means we have to take one jump from index 5 to place 55.
7 37
8 0 90
9 49 1 17
2 22
3
4
5 45
6 55
7 37
8
9 49
SEPARATE CHAINING OPEN ADDRESSING
All the keys are stored only inside the hash
Keys are stored inside the hash table as
table.
well as outside the hash table.
No key is present outside the hash table.
The number of keys to be stored in the hash The number of keys to be stored in the hash
table can even exceed the size of the hash table can never exceed the size of the hash
table. table.
Deletion is easier. Deletion is difficult.
Extra space is required for the pointers to
No extra space is required.
store the keys outside the hash table.
Cache performance is poor. Cache performance is better.
This is because of linked lists which store This is because here no linked lists are
the keys outside the hash table. used.
Some buckets of the hash table are never Buckets may be used even if no key maps
used which leads to wastage of space. to those particular buckets
Rehashing
Rehashing is a technique in which table is resized that is the size of table is double by
creating a new table. It is preferable if the total size of new table is a prime number. There
are situation in which rehashing is required.
In such situations, we have to transfer entries from old table to new table.
THANK YOU