Introduction
Searching and sorting algorithms are widely used by
developers to search data in an easier manner. Sorting
algorithms arrange the data in particular order. Searching
algorithms are used to search for data in a list. In this article,
we will learn about the binary search algorithm in detail.
Algorithm for binary search
How is an item searched in a list? When a list is inputted to the
binary search algorithm, the algorithm keeps dividing into half
until the item is matched with the one in the list.
An ordered list is a list of items that are arranged sequentially
in a particular order. For example: products on an online
shopping website are arranged according to their price and
files in Windows Explorer are arranged in various orders such
as name, size, date and type. Binary search algorithms work
faster with an ordered list.
Binary search is used for searching files in external memory in
hard disks. Let’s us search a file with file name ‘Project 625’ on
a computer. The following steps are followed by the binary
search algorithm:
i. The list of files is ordered alphabetically.
ii. The algorithm divides the list into halves and compares the
midpoint to ‘Project 625’.
iii. The algorithm finds out whether ‘Project 625’ appears
before the midpoint or after.
iv. The algorithm discards half of the list that does not contain
‘Project 625’.
v. The remaining half is divided into halves and the steps
from 2 to 4 are repeated until a single record is found
matching the name ‘Project 625’.
teachcomputerscience.co
m
The pseudocode for this scenario is:
INPUT user inputs ‘Project 625’ in File explorer
file_name=‘Project 625’
file_found=FALSE
(file_found turns TRUE only when the match is found)
WHILE file_found = FALSE:
Find the midpoint of the list
If file_name=record at midpoint of the list THEN
file_found=TRUE
ELSE IF file_name is in the first half of the list THEN
discard the second half of the list
ELSE
discard the first half of the list
OUTPUT file_name, file_size, file_data and file_type
Pseudocode
Let us consider an ordered list with a certain length_of_list.
lower_bound is the position of first element and upper_bound is
the position of the last element. In the table below, a list of 10
elements is shown. Here the lower_bound is 0, upper_bound is
9 and length_of_list is 10.
Position 0 1 2 3 4 5 6 7 8 9
Elemen
A B C D E F G H I J
t
teachcomputerscience.co
m
The pseudocode to find an element from this ordered list is:
item_to_be_found=(“enter the item to be found”)
lower_bound=0
upper_bound=length_of_list-1
item_found=false
while (item_found==false and lower_bound <= upper_bound)
midpoint= round ((lower_bound+upper_bound)/2)
if list[midpoint]=item then
item_found=true
elseif list[midpoint] < item then
lower_bound=midpoint+1
else
upper_bound=midpoint-1
endif
endwhile
if (item_found==true) then
print (“item found at ”, midpoint )
else
print (“item not present”)
endif
Let us analyse this pseudocode by using some values. Let
item_to_be_found=G in the ordered list.
Position 0 1 2 3 4 5 6 7 8 9
Elemen
A B C D E F G H I J
t
i. midpoint = round ((0+9) /2) = round (4.5) =5
list [midpoint]= list [5]= F
Because list[midpoint] < item, the statement
lower_bound=midpoint +1 is executed.
teachcomputerscience.co
m
ii. Now the lower_bound becomes 6. The lower half is now
discarded. Therefore,
Position 6 7 8 9
Elemen
G H I J
t
midpoint = round ((6+9) /2) = round (7.5) =8
list [midpoint]= list [8]= I
Because list[midpoint] > item, the statement
upper_bound=midpoint -1 is executed.
iii. Now the upper_bound becomes 7. The upper half is now
discarded. Therefore,
Position 6 7
Elemen
G H
t
midpoint = round ((6+7) /2) = round (6.5) =7
list [midpoint]= list [7]= H
Because list[midpoint] > item, the statement
upper_bound=midpoint -1 is executed.
iv. Now the upper_bound becomes 6. The upper half is now
discarded. Therefore,
Position 6
Elemen
G
t
midpoint = round ((6+6) /2) = round (6) =6
list[midpoint]= list [6] =G, the statement item_found =true
is executed.
The while loop ends and the output is: item found at 6
teachcomputerscience.co
m
Biinary Search
1. Here is a list of 9 elements.
Position 0 1 2 3 4 5 6 7 8
Elemen
P Q R S T U V W X
t
User wants to search the element P from the above list. What
steps are followed in a binary search algorithm to find out P
from the above list?
Variable names
Step 1:
Position 0 1 2 3 4 5 6 7 8
Elemen
P Q R S T U V W X
t
teachcomputerscience.co
m
Step 2:
Position
Elemen
t
Step 3:
Position
Elemen
t
teachcomputerscience.co
m
1. Using the pseudocode of binary search algorithm, create a
flowchart for this algorithm.
item_to_be_found=(“enter the item to be found”)
lower_bound=0
upper_bound=length_of_list-1
item_found=false
while (item_found==false and
lower_bound<=upper_bound)
midpoint= round ((lower_bound+upper_bound)/2)
if list[midpoint]=item then
item_found=true
elseif list[midpoint] < item then
lower_bound=midpoint+1
else
upper_bound=midpoint-1
endif
endwhile
if (item_found==true) then
print (“item found at ”, midpoint )
else
print (“item not present”)
endif
teachcomputerscience.co
m
teachcomputerscience.co
m