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

Module-6 Searching Techniques

Uploaded by

alaincage442
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module-6 Searching Techniques

Uploaded by

alaincage442
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Vasantdada Patil Pratishthan’s College of

Engineering & Visual Arts, Sion, Mumbai.

Data Structure (DS )


(CSC303)
S.E. A Div SEM-III(Computer Engineering)

MODULE NO. 6
Searching Techniques
TOPIC

Linear Search, Binary Search, Hashing-Concept, Hash Functions,


Collision resolution Technique.

11/30/2021 By Mrs. Prajakta S. Khelkar 1


Contents

• 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.

✓ The above figure shows how sequential search works. It searches an


element or value from an array till the desired element or value is not
found. If we search the element 25, it will go step by step in a sequence
order. It searches in a sequence order. Sequential search is applied on the
unsorted or unordered list when there are fewer elements in a list.
✓ Complexity to search element is - O(n)
2. Binary Search
✓ Binary Search is used for searching an element in a sorted
array.
✓ It is a fast search algorithm with run-time complexity of
O(log n).
✓ Binary search works on the principle of divide and conquer.
✓ This searching technique looks for a particular element by
comparing the middle most element of the collection.
✓ It is useful when there are large number of elements in an
array.

✓ The above array is sorted in ascending order.


✓ As we know binary search is applied on sorted lists only for
fast searching.
✓ For example, if searching an element 25 in the 7-element
array, following figure shows how binary search works:
Binary searching starts
with middle element.
If the element is equal to
the element that we are
searching then return true.
If the element is less than
then move to the right of
the list or if the element is
greater than then move to
the left of the list.
Repeat this, till you find
an element.
Implementation of Linear search
if(index==0)
#include<stdio.h> printf(“Elements %d is not found”,
#include<stdlib.h> key);
int linear(int [], int, int) else
void main() printf(“Elements %d is found at %d
{ index”, key, index);
int a[20]; }// EOM
int n, i, key, index; int linear (inta[], int size, int num)
printf(“Enter no. of elements in the {
array:”); int i, flag=0;
scanf(“%d”,&n); for(i=0;t<size; i++)
printf(“Enter elements into an {
array”); if(a[i]==num)
for(i=0;i<n;i++) {
scanf(“%d”, &a[i]); return(i);break;
printf(“Enter the element to be }//EO if
searched?”); }//EO for
scanf(“%d”,&key); If(i==size)
index= linear(a,n,key); Return flag;
11/30/2021 }//EOF 8
Implementation of Binary search
#include <stdio.h>
program
int main() while (first <= last) {
{ if (array[middle] < search)
int c, first, last, middle, n, search, first = middle + 1;
array[100]; else if (array[middle] == search) {
printf("%d found at location %d.\n",
printf("Enter number of elements\n"); search, middle+1);
scanf("%d", &n); break;
}
printf("Enter %d integers\n", n); else
last = middle - 1;
for (c = 0; c < n; c++)
scanf("%d", &array[c]); middle = (first + last)/2;
}
printf("Enter value to find\n"); if (first > last)
scanf("%d", &search); printf("Not found! %d isn't present in
the list.\n", search);
first = 0;
last = n - 1; return 0;
middle = (first+last)/2;
11/30/2021 } 9
What is a Hash Table ?

• The simplest kind of hash


table is an array of records.
• This example has 701 records.

[0] [1] [2] [3] [4] [5] [ 700]

...

An array of records
What is a Hash Table ? [ 4 ]
Number
• Each record has a special 506643548

field, called its key.


• In this example, the key is a
long integer field called
Number.

[0] [1] [2] [3] [4] [5] [ 700]

...
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.

[0] [1] [2] [3] [4] [5] [ 700]

...
What is a Hash Table ?

• When a hash table is in use, some spots contain


valid records, and other spots are "empty".

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...
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.

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...
Inserting a New Record
Number 580625685
• Typical way create a hash
value:

(Number mod 701)

What is (580625685 mod 701) ?

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...
Inserting a New Record
Number 580625685

• Typical way to create a hash


value:

(Number mod 701)


3
What is (580625685 mod 701) ?

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...
Inserting a New Record
Number 580625685
• The hash value is used for
the location of the new
record.

[3]

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 506643548 Number 155778322

...
Inserting a New Record
• The hash value is used for the location of the
new record.

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322

...
Collisions
Number 701466868
• Here is another new record
to insert, with a hash value
of 2.

My hash
value is [2].

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322

...
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.

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322

...
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.

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322

...
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.

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322

...
Collisions
• This is called a collision, because there is already
another valid record at [2].

The new record goes


in the empty spot.

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322

...
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.

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 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

• Closed hashing = Open addressing


11/30/2021 34
11/30/2021 37
11/30/2021 38
11/30/2021 39
11/30/2021 40
11/30/2021 43
11/30/2021 44

You might also like