0% found this document useful (0 votes)
2 views10 pages

Modue 5

The document outlines various searching algorithms including Sequential Search, Binary Search, and Hashing techniques, detailing their input, output, and operational steps. It explains the time complexities associated with these algorithms and introduces different hash functions and collision resolution methods such as closed and open hashing. Additionally, it provides a performance comparison of various sorting algorithms, highlighting their best, average, and worst-case scenarios.

Uploaded by

shilpasivadas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Modue 5

The document outlines various searching algorithms including Sequential Search, Binary Search, and Hashing techniques, detailing their input, output, and operational steps. It explains the time complexities associated with these algorithms and introduces different hash functions and collision resolution methods such as closed and open hashing. Additionally, it provides a performance comparison of various sorting algorithms, highlighting their best, average, and worst-case scenarios.

Uploaded by

shilpasivadas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

MODULE 5

SEQUENTIAL SEARCH
Sequential_search(key)
Input: An unsorted array a[], n is the no.of elements, key indicates the
element to be searched
Output: Target element found status
DS: Array
1. Start
2. i=0
3. flag=0
4. While i<n and flag=0
1. If a[i]=key
1. Flag=1
2. Index=i
2.end if
3. i=i+1
5. end while
6. if flag=1
1. print “the key is found at location index”
7. else

1. print “key is not found”


7. end if
8. stop
Analysis
In this algorithm the key is searched from first to last position in linear manner. In the
case of a successful search, it search elements up to the position in the array where it
finds the key. Suppose it finds the key at first position, the algorithm behaves like best
case, If the key is at the last position, then algorithm behaves like worst case. Thus the
worst case time complexity is equal to the no. of comparison at worst case ie., equal to
O(n). The time complexity in best case is O(1).
The average case time complexity =( no. of comparisons required when the key is in
the first position + no. of comparisons required when the key is in second position+…+
no. of comparison when key is in nth position)/n
1 + 2 + n(n 
….n 1) =O(n)
n
= 2n

Department of Computer Science &


Engineering
Binary Search
Binary Search(key)
Input: An unsorted array a[], n is the no.of elements, key indicates the element to be
searched
Output: Target element found status
DS: Array
1. Start
2. Start=0,end=n-1
3. Middle=(start+end)/2
4. While key!=a[middle] and start<end
1. If key>a[middle]
1. Start=middle+1
2.else
1. end= middle-1
3. end if
4. middle=(start+end)/2
5. end while
6. if key=a[middle]
1. print “the key is found at the position”
7. else
1. print “the key is not found”
8. end if
9. stop
HASHING
We have seen about different search techniques (linear search, binary search) where
search time is basically dependent on the no of elements and no. of comparisons
performed.
Hashing is a technique which gives constant search time. In hashing the key
value is stored in the hash table depending on the hash function. The hash function maps
the key into corresponding index of the array(hash table). The main idea behind
any hashing technique is to findone-to-one
correspondence between a key value and an index in the hash table where the key value
can be placed. Mathematically, this can be expressed as shown in figure below where K
denotes a set of key values, I denotes a range of indices, and H denotes the mapping
function from K to I.

Department of Computer Science &


Engineering
All key values are mapped into some indices and more than one key value may be
mapped into an index value. The function that governs this mapping is called the hash
function. There are two principal criteria in deciding hash function H:K->I as follows.
1) The function H should be very easy and quick to compute
2) It should be easy to implement

As an example let us consider a hash table of size 10 whose indices are 0,1,2,…
9.Suppose a set of key values are 10,19,35,43,62,59,31,49,77,33.Let us assume the
hash function as stated below
1) Add the two digits in the key
2) Take the digit at the unit place of the result as index , ignore the digits at tenth
place if any

Using this hash function, the mapping from key values to indices and to hash tables are
shown below.

HASH FUNCTIONS
There are various methods to define hash function
Division method
One of the fast hashing functions, and perhaps the most widely accepted, is the
division method, which is defined as follows:
Choose a number h larger than the number n of keys in K. The hash function H is
then defined by
H(k)=k(MOD h) if indices start from 0
Or
H(k)=k(MOD h)+1 if indices start from 1

Department of Computer Science &


Engineering
Where k€K, a key value. The operator MOD defines the modulo arithmetic operator
operation, which is equal to dividing k by h. For example if k=31 and h=13 then,
H(31)=31 MOD 13=5 (OR)
H(31)=31( MOD 13)+1=6
h is generally chosen to be a prime number and equal to the sizeof hash table

MID SQUARE METHOD


Another hash function which has been widely used in many applications is the mid
square method. The hash function H is defined by H(k)=x, where x is obtained by
selecting an appropriate number of bits or digits from the middle of the square of the
key value k. example-
k : 1234 2345 3456

k2 : 1522756 5499025 11943936


H(k) : 525 492 933
For a three digit index requirement, after finding the square of key values,the digits at

2nd, 4th and 6th position are chosen as their hash values.

FOLDING METHOD

Another fair method for a hash function is folding method. In this method, the

key k is partitioned into a number of parts k 1 , k2..kn where each part has equal no.of
digits as the required address(index) width. Then these parts are added together in the
hash function.
H(k)=k1+k2+…+kn. Where the last carry, if any is ignored. There are mainly two
variations of this method.
1) fold shifting method
2) fold boundary method
Fold Shifting Method
In this method, after the partition even parts like k2, k4 are reversed before
addition.
Fold boundary method
In this method, after the partition the boundary parts are reversed before
addition
Example
-Assume size of each part is 2 then, the hash function is computed as follows

Key values 1522756 5499025 11943936


k:
Chopping : 01 52 27 56 05 49 90 25 11 94 39 36
Pure folding : 01+52+27+56=136 05+49+90+25=169 11+94+39+36=180
Fold 10+52+72+56=190 50+49+09+25=133 11+94+93+36=234
Shifting:
Fold :10+52+27+65=154 50+49+90+52=241 11+94+39+63=20
Department of Computer Science &
Engineering
Boundary 7

Department of Computer Science &


Engineering
DIGIT ANALYSIS METHOD
This method is particularly useful in the case of static files where the key values of all
the records are known in advance. The basic idea of this hash function is to form hash
address by extracting and/or shifting the extracted digits of the key. For any given set of
keys, the position in the keys and the same rearrangement pattern must be used
consistently. The decision for extraction and rearrangement is finalized after analysis of
hash functions under different criteria.
Example: given a key value 6732541, it can be transformed to the hash address 427
by extracting the digits from even position. And then reversing this combination.ie 724
is the hash address.
Collision resolution and overflow handling techniques
There are several methods to resolve collision. Two important methods are
listed below:
1) Closed hashing(linear probing)
2) Open hashing
(chaining) CLOSED HASHING
Suppose there is a hash table of size h and the key value is mapped to location
i, with a hash function. The closed hashing can then be stated as follows.
Start with the hash address where the collision has occurred,let it be i.
Then carry out a sequential search in the order:- i, i+1,i+2..h,1,2…,i-1 The
search will continue until any one of the following occurs
 The key value is found
 An unoccupied location is found
 The searches reaches the location where search had started
The first case corresponds to successful search , and the other two case corresponds to
unsuccessful search.Here the hash table is considered circular, so that when the last
location is reached, the search proceeds to the first location of the table. This is why the
technique is termed closed hashing. Since the technique searches in a straight line, it is
alternatively termed as linear probing.
Example- Assume there is a hash table of size 10 and hash function uses the division
method of remainder modulo 7, namely H(k)=k(MOD 7)+1.The construction of hash
table for the key values 15,11,25,16,9,8,12,8 is illustrated below.

Department of Computer Science &


Engineering
Drawback of closed hashing and its remedies
The major drawback of closed hashing is that as half of the hash table is filled,
there is a tendency towards clustering.That is key values are clustered in large
groups.and as a result sequential search becomes slower and slower. This kind of
clustering is known as primary clustering.
The following are some solutions to avoid this situation
1)Random probing
2)Double hashing
3)Quadratic probing
Random Probing
Instead of using linear probing that generates sequential locations in order, a
random location is generated using random probing.
An example of pseudo random number generator that generates such a
random sequence is given below:
I=(i+m)MOD h+1
Where m and h are prime numbers. For example if m=5, and h=11 and initially=2
then random probing generates the sequence
8,3,9,4,10,5,11,6,1,7,2
Here all numbers are generated between 1 and 11 in a random order. Primary
clustering problem is solved. Where as there is an issue of clustering when two keys
are hashed into the same location and then they make use of the same sequence
locations generated by the random probing, which is called as secondary clustering

Department of Computer Science &


Engineering
Double Hashing
An alternative approach to solve the problem of secondary clustering is to make use of
second hash function in addition to the first one. Suppose H1(k) is initially used hash
function and H2(k) is the second one. These two functions are defined as
H1(k)=(k MOD h)+1 H2(k)=(k
MOD (h-4))+1
Let h=11, and k=50 for an instance, then H1(50)=7
and H2(50)=2.
Now let k=28, then H1(28)=7 and H2(28)=5
Thus for the two key values hashing to the same location, rehashing generates two
different locations alleviating the problem of secondary clustering.
Quadratic Probing
It is a collision resolution method that eliminates the primary clustering
problem of linear probing. For linear probing, if there is a collision at location i, then the
next locations i+1, i+2..etc are probed.But in quadratic probing next locations to be

probed are i+12 ,i+22 ,i+32 ..etc . This method substantially reduces primary clustering,
but it doesn’t probe all the locations in the table.

Open Hashing
Closed hashing method for collision resolution deals with arrays as hash tables and thus
random positions can be quickly refer red. Two main disadvantages of closed hashing
are
1) It is very difficult to handle the problem of overflow in a satisfactory manner
2) The key values are haphazardly intermixed and, on the average majority of the
key values are from their hash locations increasing the number of probes which
degrades the overall performance
To resolve these problems another hashing method called open hashing or separate
chaining is used.
The chaining method uses hash table as an array of pointers. Each pointer points a
linked list. That is here the hash table is an array of list of headers. Illustrated below is
an example with hash table of size10.

Department of Computer Science &


Engineering
For searching a key in hash table requires the following steps 1)Key is
applied to hash function
2) Hash function returns the starting address of a particular linked list(where
key may be present)
3) Then key is searched in that linked list

Performance Comparison Expected

Algorithm Best Case Average Case Worst Case


Name
Quick Sort O(n log n) O(n log n) O(n 2 )
Merge Sort O(n log n) O(n log n) O(n log n)
Heap Sort O(n log n) O(n log n) O(n log n)
Bubble Sort O(n) O(n 2 ) O(n 2 )
Selection Sort O(n 2 ) O(n 2 ) O(n 2 )
Insertion Sort O(n) O(n 2 ) O(n 2 )
Binary Search O(1) O(log n) O(log n)
Linear Search O(1) O(n) O(n)

Department of Computer Science &


Engineering
Department of Computer Science &
Engineering

You might also like