Module-6 Searching Techniques
Module-6 Searching Techniques
MODULE NO. 6
Searching Techniques
TOPIC
• Linear Search,
• Binary Search,
• Hashing-Concept,
• Hash Functions,
• Collision resolution Techniques
11/30/2021 2
Searching
What is Searching?
◼ Searching is the process of finding a given value position in a
list of values.
◼ It decides whether a search key is present in the data or not.
◼ It is the algorithmic process of finding a particular item in a
collection of items.
◼ It can be done on internal data structure or on external data
structure.
Searching refers to determining weather an element is present in
a given list of elements or not.
List of elements could be represented using an:
• Array
• Linked list
• Binary tree
• B tree
11/30/2021 3
When is a search fruitful?
• If the element is found to be present in a list then the search is
considered as a successful, otherwise it is considered as an
unsuccessful search.
• The search operation returns the location or address of the
element found.
• There are various searching methods available.
On what basis to select a method?
• The choice of particular searching method depends upon the
following factors:
1) Order of elements in the list i.e. sorted & unsorted.
2) Size of the list.
• The two searching methods are:
1) Linear Search 2) Binary Search
11/30/2021 4
1. Sequential Search
✓ Sequential search is also called as Linear Search.
✓ Sequential search starts at the beginning of the list and checks every
element of the list.
✓ It is a basic and simple search algorithm.
✓ Sequential search compares the element with all the other elements given
in the list.
✓ If the element is matched, it returns the value index, else it returns -1.
...
An array of records
What is a Hash Table ? [ 4 ]
Number
• Each record has a special 506643548
...
What is a Hash Table ? [4]
Number 506643548
• The number might be a
person's identification
number, and the rest of the
record has information about
the person.
...
What is a Hash Table ?
...
Inserting a New Record
Number 580625685
• In order to insert a new
record, the key must
somehow be converted to an
array index.
• The index is called the hash
value of the key.
...
Inserting a New Record
Number 580625685
• Typical way create a hash
value:
...
Inserting a New Record
Number 580625685
...
Inserting a New Record
Number 580625685
• The hash value is used for
the location of the new
record.
[3]
...
Inserting a New Record
• The hash value is used for the location of the
new record.
...
Collisions
Number 701466868
• Here is another new record
to insert, with a hash value
of 2.
My hash
value is [2].
...
Collisions
• This is called a collision, Number 701466868
because there is already
another valid record at [2].
When a collision
occurs,
move forward until you
find an empty spot.
...
Collisions
• This is called a collision, Number 701466868
because there is already
another valid record at [2].
When a collision
occurs,
move forward until you
find an empty spot.
...
Collisions
Number 701466868
• This is called a collision,
because there is already
another valid record at [2].
When a collision
occurs,
move forward until you
find an empty spot.
...
Collisions
• This is called a collision, because there is already
another valid record at [2].
...
Hash Concept
• The advantage of using hashing is that
the table address of a record can be
directly computed from the key.
11/30/2021 24
11/30/2021 25
Hashing function
1) Division Method:
L= K mod M
L= Location in Table
K= Key
M= Size of Table
E.g.: L=23 Mod 10
=3
The key whose value is 23 is placed in 3rd location.
11/30/2021 26
2) Mid square Method: Square the value of a key and
the no. Of digits required to form an address, from the
middle part of the squared value. E. g.: Key 12, then
square is 144. To get two digits address (index) 44 is
selected.
11/30/2021 27
11/30/2021 28
Hash Concept
11/30/2021 29
11/30/2021 30
2) Mid square Method:
Square the value of a key and the no. Of digits required to
form an
address, from the middle part of the squared value.
E. g.: Key 12, then square is 144.
To get two digits address (index) 44 is selected.
3) Folding Method:
In this method, the Key is partitioned into no. Of parts,
k1,k2,…..,kr.
Then parts are added together ignoring carry.
H(k)= k1+ k2+ ….+ kr
Eg.1.: H(3205)= 32+05= 37
Eg.2: H(8976226221)= 89+76+22+62+21=270 ->70 (carry 2
is ignored)
11/30/2021 31
11/30/2021 32
Collisions and their Resolution
• A collision occurs when two different keys hash to the same
value
– E.g. For TableSize = 17, the keys 18 and 35 hash to the
same value
– 18 mod 17 = 1 and 35 mod 17 = 1
• Cannot store both data records in the same slot in array!
• Two different methods for collision resolution:
– Separate Chaining: Use a dictionary data structure (such
as a linked list) to store multiple items that hash to the
same slot
– Closed Hashing (or probing): search for empty slots
using a second function and store item in first empty slot
that is found
33
• Separate chaining = Open hashing