You are on page 1of 20

SEARCHING

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

Output (if found)

Output (if NOT found)


SAMPLE CODE WHERE VALUES OF THE ARRAY
COMES FROM THE USER. Sample Program #2

Output (if found)

Output (if NOT found)


BINARY
SEARCH
• Binary Search is referred to as the
"divide and conquer" approach.
• This algorithm is specifically
designed for searching in sorted
data-structures.
• These type of searching algorithms ▪ If the data given by the user is stored in the array
are much more efficient than Linear then the array will have three variable to name as
Search as they repeatedly target the first, last and middle. Each of the three variables
center of the search structure and are used in searching of the given array.
divide the search space in half. ▪ The flaw of a binary search is that it could only be
applied on a sorted array. We cannot apply it on an
• The time complexity of binary unsorted array. This is a very useful technique
search algorithm is O(Log n). because it is used to quickly found the required
values.
ALGORITHM FOR
BINARY 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 first be the
starting index, last the highest
index and mid the middle
index.
Sample Program #3
SAMPLE CODE WHERE VALUES
OF THE ARRAY COMES FROM
THE USER.

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

Given an array Q containing n elements. Ask the user to


enter elements in integer value and sort the elements
then ask for the value to be located in the array using
linear searching and display whether the value is
located or not.
TEST CASE

Given an array Q containing n elements. Ask the


user to enter elements in integer value and sort
the elements then ask for the value to be
located in the array using binary searching and
display whether the value is located or not.

You might also like