0% found this document useful (0 votes)
3 views

Unit-9-Searching

The document discusses various searching techniques, including Linear Search and Binary Search, explaining their algorithms and efficiency. It also covers hashing as an efficient searching method, detailing different hash functions and collision resolution techniques. Key concepts such as hash tables and methods for minimizing collisions, like linear probing, are also presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit-9-Searching

The document discusses various searching techniques, including Linear Search and Binary Search, explaining their algorithms and efficiency. It also covers hashing as an efficient searching method, detailing different hash functions and collision resolution techniques. Key concepts such as hash tables and methods for minimizing collisions, like linear probing, are also presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit -9 Searching

Searching

Searching is a process of finding an element within the list of elements stored in any order or
randomly. It is an operation or technique that helps finds the place of a given element or value in
the list. Any search is said to be successful or unsuccessful depending upon whether the element
that is being searched is found or not. Some of the standard searching technique that is being
followed in the data structure are:

 Linear Search or Sequential Search


 Binary Search.

Sequential Search

In sequential search, access each element of an array one by one sequentially and see whether it
is desired element or not. A search will be unsuccessful if all the elements are accessed and the
desired element is not found.

In brief, simply search for the given element left to right and return the index of the element, if
found. Otherwise return “Not Found”.

Algorithm:

1. Start
2. Read the search element from the user
3. Compare, the search element with the first element in the list.
4. If both are matching, then display "Given element found!!!" and terminate the function
5. If both are not matching, then compare search element with the next element in the list.
6. Repeat steps 4 and 5 until the search element is compared with the last element in the list.
7. If the last element in the list is also doesn't match, then display "Element not found!!!" and
terminate the function
8. Stop
Binary search
Binary search is a fast search algorithm with run-time complexity of Ο (log n). This search
algorithm works on the principle of divide and conquers. For this algorithm to work properly,
the data collection should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item of the collection. If
a match occurs, then the index of item is returned. If the middle item is greater than the item,
then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is
searched for in the sub-array to the right of the middle item. This process continues on the sub-
array as well until the size of the sub array reduces to zero.

How Binary Search Works?

For a binary search to work, it is mandatory for the target array to be sorted. We shall learn the
process of binary search with a pictorial example. The following is our sorted array and let us
assume that we need to search the location of value 31 using binary search.

First, we shall determine half of the array by using this formula −

mid = 0+9/ 2

Here it is, 0+9/2= 4 (integer value of 4.5). So, 4 is the mid of the array.

Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find
that the value at location 4 is 27, which is not a match. As the value is greater than 27 and we
have a sorted array, so we also know that the target value must be in the upper portion of the
array.
mid = 5+9/ 2

Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.

The value stored at location 7 is not a match; rather it is more than what we are looking for. So,
the value must be in the lower part from this location.

Hence, we calculate the mid again. This time it is 5.

We compare the value stored at location 5 with our target value. We find that it is a match.

We conclude that the target value 31 is stored at location 5.


Algorithm

1. Start
2. Read the search element from the user
3. Find the middle element in the sorted list
4. Compare, the search element with the middle element in the sorted list.
5. If both are matching, then display "Given element found!!!" and terminate the function
6. If both are not matching, then check whether the search element is smaller or larger than
middle element.
7. If the search element is smaller than middle element, then repeat steps 2, 3, 4 and 5 for the
left sub list of the middle element.
8. If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for the
right sub list of the middle element.
9. Repeat the same process until we find the search element in the list or until sub list contains
only one element.
10. If that element also doesn't match with the search element, then display "Element not found
in the list!!!" and terminate the function.
11. Stop.

Efficiency
From the above algorithm we can say that the running time of the algorithm is
T(n) = T(n / 2) + O(1)
By solving this we get O (log n)
In the best case output is obtained at one run i.e. O(1) time if the key is at middle.
In the worst case the output is at the end of the array so running time is O(log n) time.
In the average case also running time is O(log n)..
Hashing

It is an efficient searching technique in which key is placed in direct accessible address for rapid
search. Hashing provides the direct access of records from the file no matter where the record is
in the file. Due to which it reduces the unnecessary comparisons. This technique uses a hashing
function say h which maps the key with the corresponding key address or location.

A function that transforms a key into a table index is called a hash function.

A common hash function is

h(x)=x mod SIZE

if key=27 and SIZE=10 then

hash address=27%10=7

Types of Hash Functions

There are different types of hash functions are used some of them are described below:

Division Method
This is the easiest method to create a hash function. The hash function can be described as −

h(k) = k mod n
Here, h(k) is the hash value obtained by dividing the key value k by size of hash table n using
the remainder. It is best that n is a prime number as that makes sure the keys are distributed with
more uniformity.

An example of the Division Method is as follows −

k=1276
n=10
h(1276) = 1276 mod 10
=6
The hash value obtained is 6

Multiplication Method
The hash function used for the multiplication method is −

h(k) = floor( n( kA mod 1 ) )


Here, k is the key and A can be any constant value between 0 and 1. Both k and A are multiplied
and their fractional part is separated. This is then multiplied with n to get the hash value.
An example of the Multiplication Method is as follows −

k=123
n=100
A=0.618033
h(123) = 100 (123 * 0.618033 mod 1)
= 100 (76.018059 mod 1)
= 100 (0.018059)
=1
The hash value obtained is 1

Mid Square Method


The mid square method is a very good hash function. It involves squaring the value of the key
and then extracting the middle r digits as the hash value. The value of r can be decided
according to the size of the hash table.
An example of the Mid Square Method is as follows −
Suppose the hash table has 100 memory locations. So r=2 because two digits are required to
map the key to memory location.
k = 50
k*k = 2500
h(50) = 50
The hash value obtained is 50

Hash Tables

A hash table is a data structure that maps keys to values. It uses a hash function to calculate the
index for the data key and the key is stored in the index.

An example of a hash table is as follows

The key sequence that needs to be stored in the hash table is

35 50 11 79 76 85

The hash function h(k) used is:

h(k) = k mod 10
Hash Collision:

If two or more than two records trying to insert in a single index of a hash table then such a
situation is called hash collision.

Collision Resolution

When two items hash to the same slot, we must have a systematic method for placing the second
item in the hash table. This process is called collision resolution. If the hash function is perfect,
collisions will never occur. However, since this is often not possible, collision resolution
becomes a very important part of hashing. Some popular methods for minimizing collision are:

✔ Linear probing

✔ Quadratic probing

✔ Rehashing

✔ Chaining

✔ Hashing using buckets etc

Linear probing: A hash-table in which a collision is resolved by putting the item in the next empty
place within the occupied array space. It starts with a location where the collision occurred and does a
sequential search through a hash table for the desired empty location. Hence this method searches in
straight line, and it is therefore called linear probing.

Example: Insert keys {89, 18, 49, 58, 69} with the hash function h(x)= x mod 10 using linear probing
Solution:

You might also like