6 Searching Algorithms
6 Searching Algorithms
ALGORITHMS
MCS 201-Data Structure and Algorithms
Rosalyn P. Reyes
Searching Algorithms are designed to
check for an element or retrieve an
SEARCHING element from any data structure where it is
stored.
ALGORITHMS
Based on the type of search operation, these algorithms
are generally classified into two categories:
1. Linear Search
2. Binary Search
LINEAR SEARCH
• In computer science, a linear
search algorithm or sequential
search is a method for finding an
element within a list. It sequentially
checks each element of the list
until a match is found or the whole
list has been searched. In case of an array we check that the given key or a number is
present in array at any index or not by comparing each element
• Linear search or sequential search of array
is one of the searching algorithm in
which we have some data in a data
There can be two possible outcomes if we are assuming that data
structure like array data structure
and we have to search a particular structure like array contains unique values.
element in. Linear search successful Key Found means in array at an
index we found value which matches to our key
• By traversing the whole data Linear search failed to find the Key mean our key does not
structure elements from start to
end one by one to find key exist in data
comparing with each data
structure element to the key.
1. Initialize the array with the values 2, 4, 6, 8 and 10.
ALGORITHM FOR 2. Ask the user to enter an element to search in the array.
key = userInput
3. Compare the given key element to each and every element
LINEAR SEARCHING in array
for (ctr = 0; ctr < size; ctr++)
if (key == array(ctr))
Assume that the name of the
4. If element is present in the given array, then display the
array is num. Let key be the position of the element. Set flag into 1 which pertains that the
value to be searched on the key is found in the array.
array. Let flag determine if the flag = 1
index = ctr
key exist in the array and index if (flag == 1)
be the index of the key if found Display key found and the index of the
in the list. key
Else
Display key not found!
Sample Program #1
Sample Program #4
Output (if found) Output (if NOT found)
INTERPOLATION
SEARCH
• Interpolation search (sometimes
referred to as extrapolation ▪ It parallels how humans search through a telephone
search) is an algorithm for book for a particular name, the key value by which the
searching for a given key value in book's entries are ordered.
an indexed array that has been
ordered by the values of the key.
In each search step it calculates where in the remaining
• Is an improvement over Binary search space the sought item might be, based on the key
Search for instances, where the values at the bounds of the search space and the value of
values in a sorted array are the sought key. The key value actually found at this
uniformly distributed. Interpolation estimated position is then compared to the key value being
constructs new data points within sought. If it is not equal, then depending on the
the range of a discrete set of known comparison, the remaining search space is reduced to the
data points. Binary Search always part before or after the estimated position. This method
goes to the middle element to will only work if calculations on the size of differences
check between key values are sensible.
ALGORITHM FOR
INTERPOLATION
SEARCHING
Assume that the name of the
array is num. Let key be the
value to be searched on the
array. Let flag determine if the
key exist in the array and index
be the index of the key if found
in the list. Also, let low be the
starting index, high the highest
index and mid be the starting
index to start the search.
Sample Program #5
PRACTICE EXERCISE