0% found this document useful (0 votes)
12 views3 pages

Experiment No.10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Experiment No.10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment No.

10
Aim: Write a program to implement linear search and binary search.

Objectives:
1) To understand the concept of searching.
2) To implement linear search and binary search.
3) To analyse time complexity of linear and binary search.

Theory:
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). For example, if an array A[]
is declared and initialized as,
int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5};
and 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). Figure 10.1 shows the algorithm for linear search.

Figure 10.1 Algorithm for Linear Search


In Steps 1 and 2 of the algorithm, we initialize the value of POS and I. In Step 3, a while loop is
executed that would be executed till I is less than N (total number of elements in the array). In Step
4, a check is made to see if a match is found between the current array element and VAL. If a match
is found, then the position of the array element is printed, else the value of I is incremented to
match the next element with VAL. However, if all the array elements have been compared with VAL
and no match is found, then it means that VAL is not present in the array.
Complexity of Linear Search Algorithm
Linear search executes in O(n) time where n is the number of elements in the array. Obviously, the
best case of linear search is when VAL is equal to the first element of the array. In this case, only
one comparison will be made. Likewise, the worst case will happen when either VAL is not present
in the array or it is equal to the last element of the array. In both the cases, n comparisons will have
to be made. However, the performance of the linear search algorithm can be improved by using a
sorted array.
Binary Search
Binary search is a searching algorithm that works efficiently with a sorted list. The mechanism of
binary search can be better understood by an analogy of a telephone directory. When we are
searching for a particular name in a directory, we first open the directory from the middle and then
decide whether to look for the name in the first part of the directory or in the second part of the
directory. Again, we open some page in the middle and the whole process is repeated until we
finally find the right name.
Now, let us consider how this mechanism is applied to search for a value in a sorted array.
Consider an array A[] that is declared and initialized as
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
and the value to be searched is VAL = 9. The algorithm will proceed in the following manner.
BEG = 0, END = 10, MID = (0 + 10)/2 = 5
Now, 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
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.
In this algorithm, we see that BEG and END are the beginning and ending positions of the segment
that we are looking to search for the element. MID is calculated as (BEG + END)/2.
Initially, BEG = lower_bound and END = upper_bound. The algorithm will terminate when A[MID] =
VAL. When the algorithm ends, we will set POS = MID. POS is the position at which the value is
present in the array.
However, if VAL is not equal to A[MID], then the values of BEG, END, and MID will be changed
depending on whether VAL is smaller or greater than A[MID].
(a) If VAL < A[MID], then VAL will be present in the left segment of the array. So, the value of END
will be changed as END = MID – 1.
(b) (b) If VAL > A[MID], then VAL will be present in the right segment of the array. So, the value of
BEG will be changed as BEG = MID + 1.
Finally, if VAL is not present in the array, then eventually, END will be less than BEG. When this
happens, the algorithm will terminate and the search will be unsuccessful.
Figure 10.2 shows the algorithm for binary search.
In Step 1, we initialize the value of variables, BEG, END, and POS. In Step 2, a while loop is executed
until BEG is less than or equal to END. In Step 3, the value of MID is calculated. In Step 4, we check
if the array value at MID is equal to VAL (item to be searched in the array). If a match is found, then
the value of POS is printed and the algorithm exits. However, if a match is not found, and if the value
of A[MID] is greater than VAL, the value of END is modified, otherwise if A[MID] is greater than VAL,
then the value of BEG is altered. In Step 5, if the value of POS = –1, then VAL is not present in the
array and an appropriate message is printed on the screen before the algorithm exits.

Figure 10.1 Algorithm for Binary Search


Complexity of Binary Search Algorithm
The complexity of the binary search algorithm can be expressed as f(n), where n is the number of
elements in the array. The complexity of the algorithm is calculated depending on the number of
comparisons that are made. In the binary search algorithm, we see that with each comparison, the
size of the segment where search has to be made is reduced to half. Thus, we can say that, in order
to locate a particular value in the array, the total number of comparisons that will be made is given
as 2f(n) > n or f(n) = log2 n

You might also like