Data Structures
Div B
SY EXCP
Sem III
Dr. Rajashree Daryapurkar
2023-24
Searching Algorithms
Searching
• Searching means to find whether a particular
value is present in an array or not.
• If the value is present in the array, then searching
is said to be successful and the searching process
gives the location of that value in the array.
• if the value is not present in the array, the
searching process displays an appropriate
message and in this case, searching is said to be
unsuccessful
Linear search
• Linear search, also called as sequential search,
is a very simple method used for searching an
array for a particular value.
• It works by comparing the value to be
searched with every element of the array one
by one in a sequence until a match is found.
• Linear search is mostly used to search an
unordered list of elements (array in which
data elements are not sorted).
Algorithm for linear search
Step 1: [INITIALIZE] SET POS = -1
Step 2: [INITIALIZE] SET I = 1
Step 3: Repeat Step 4 while I<=N
Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
Algorithm for linear search
SET I = I + 1
[END OF LOOP]
Step 5: IF POS = –1
PRINT VALUE IS NOT PRESENT
IN THE ARRAY
[END OF IF]
Step 6: EXIT
Linear search
• For example, if an array A[] is declared and
initialized as, int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6,
5};
• The value to be searched is VAL = 7, then
searching means to find whether the value ‘7’
is present in the array or not.
• If yes, then it returns the position of its
occurrence.
• Here, POS = 3 (index starting from 0).
Binary search
• Binary search is a searching algorithm that
works efficiently with a sorted list.
• Analogy of dictionary
Binary search Algorithm
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
and the value to be searched is VAL = 9
BEG = 0, END = 10, MID = (0 + 10)/2 = 5
VAL = 9 and A[MID] = A[5] = 5
A[5] is less than VAL, therefore, we now search for
the value in the second half of the array. So,
we change the values of BEG and MID.
Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2
=16/2 = 8
Binary search Algorithm
VAL = 9 and A[MID] = A[8] = 8
A[8] is less than VAL, therefore, we now search
for the value in the second half of the segment.
So, again we change the values of BEG and MID.
Now, BEG = MID + 1 = 9, END = 10, MID = (9 +
10)/2 = 9
Now, VAL = 9 and A[MID] = 9.
Binary search Algorithm
Step 1: [INITIALIZE] SET BEG = lower_bound
END = upper_bound, POS = - 1
Step 2: Repeat Steps 3 and 4 while BEG <= END
Step 3: SET MID = (BEG + END)/2
Step 4: IF A[MID] = VAL
SET POS = MID
PRINT POS
Binary search Algorithm
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]
Binary search Algorithm
Step 5: IF POS = -1
PRINT “VALUE IS NOT PRESENT IN THE ARRAY”
[END OF IF]
Step 6: EXIT
Hashed list Search
• Hashing in data structure uses hash tables to
store the key-value pairs.
• The hash table then uses the hash function to
generate an index.
• Hashing uses this unique index to perform
insert, update, and search operations.
• It can be defined as a bucket where the data
are stored in an array format.
Hashing
• Hashing refers to the process of generating a
fixed-size output from an input of variable size
using the mathematical formulas known as
hash functions.
• This technique determines an index or
location for the storage of an item in a data
structure.
Hashing
Hashing
• Key: A Key can be anything string or integer which is fed as
input in the hash function the technique that determines
an index or location for storage of an item in a data
structure.
• Hash Function: The hash function receives the input
key and returns the index of an element in an array called a
hash table. The index is known as the hash index.
• Hash Table: Hash table is a data structure that maps keys to
values using a special function called a hash function. Hash
stores the data in an associative manner in an array where
each data value has its own unique index.
Links for searching algorithms
• [Link]