Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Searching Algorithm

Advanced Level
CC By Jacob Zvirikuzhe
Objective(s)
Explain the steps of writing searching algorithm.
Write searching algorithms for linear and binary searches.
What is a searching algorithm?
• A search algorithm is the step-by-step procedure used to locate an array
element among a collection of elements. Examples of Searching
Algorithms are:

(a) Linear Search Algorithms


(b) Binary Search Algorithms
Linear Search Algorithm
• The search is made through all items one by one. Every item is checked
and if a match is found then that particular item is returned, otherwise
the search continues till the end of the data collection.
Linear Search Algorithm
Linear Search Algorithm
The Linear Search Pseudocode
PROCEDURE linearSearch (list, value)
FOR each item in list
IF match item == value
RETURN item location
END IF
END FOR
END PROCEDURE
Binary Search
• The binary search method is used for searching an ordered array. It is
a divide and conquer algorithm.
Binary Search
• First, we divide the array elements by half using the formula,
Mid = (low + high)/2 where low and high are the lowest index and
highest index respectively.
Mid = (0 + 4) / 2
Mid = 2
Binary Search
• First, we divide the array elements by half using the formula,
Mid = (low + high)/2 where low and high are the lowest index and highest index
respectively.
Mid = (0 + 4) / 2
Mid = 2

There is no 17 on the midpoint but the value on the midpoint is less than 17 so we
are quite sure that the lower portion does not have the value that we are looking for.
Binary Search

• We change our low point to Mid +1


Mid = (low + high)/2
Mid = (3 + 4) / 2
Mid = 3,5
Mid = 3
Binary Search

17 has been located on Index 3.

Binary search can only be


done on a sorted array. The
search value can only be
found on the midpoint.
• Binary Search Pseudocode
Procedure binary_search (A, x)
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
WHILE x not found if upperBound < lowerBound
EXIT:
Print “x does not exist.”
SET midPoint = (lowerBound + upperBound ) / 2
if A[midPoint] = x EXIT:
print “x found at location” midPoint
Elseif A[midPoint] < x
set lowerBound = midPoint + 1
Elseif A[midPoint] > x
set upperBound = midPoint - 1
END WHILE
END PROCEDURE
Activity 1
1. Use Binary Search to search for the value 14 from the array below.
15 23 13 22 27 10 21 14 30 18
2. The following pseudocode processes a 1D array called MyNumbers. The numbers in MyNumbers are sorted
and x, Low,High and mid are integer variables
PROCEDURE D(Low, High, x)
Found = False
REPEAT
Mid = (INT (Low + High) /2 ))
IF MyNumbers(Mid) = x Then
Found = True
ELSE IF MyNumbers (Mid) > x THEN
High = Mid – 1
ELSE
Low = Mid + 1 {MyNumbers(Mid) < x}
UNTIL Found = True
END PROCEDURE
Dry run the algorithm above using the trace table below for D(1,10,19) given that the integers in the list are:
15 23 13 22 27 10 21 14 30 18
Activity 2
Create an algorithm for the following scenario.
A number guessing game is played by two players, A
and B. Player A inputs ten integers in any order. Player
B will input guess number. The algorithm should
search through the entire array to locate the number
and return the result.
Summary
Sorting algorithms locate an element on an array.
Examples of searching algorithms are linear search and
binary search.
In linear search, the search process is made through all
items one by one.
Binary search divides the array into subarrays by
calculating the midpoint.
Binary search is effective when the list has been sorted.
The End

You might also like