0% found this document useful (0 votes)
2 views10 pages

U3 L1 Searching

The document introduces searching as a method to find values in an array, detailing two main techniques: linear search and binary search. Linear search checks each element sequentially and has a time complexity of O(n), while binary search operates on sorted arrays and reduces the search space by half with each comparison, resulting in a complexity of log2n. The document provides examples and explanations for both searching methods and their respective complexities.

Uploaded by

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

U3 L1 Searching

The document introduces searching as a method to find values in an array, detailing two main techniques: linear search and binary search. Linear search checks each element sequentially and has a time complexity of O(n), while binary search operates on sorted arrays and reduces the search space by half with each comparison, resulting in a complexity of log2n. The document provides examples and explanations for both searching methods and their respective complexities.

Uploaded by

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

Introduction to

searching
Searching means to find whether a particular value is present in an
array or not. If the value is present in the array, then searching is said
to be successful and the searching process gives the location of that
value in the array. However, if the value is not present in the array,
the searching process displays an appropriate message and in this
case searching is said to be unsuccessful.

There are two popular methods for searching the array elements:
1. linear search
2. binary search.
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};
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.
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.
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
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.

(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) 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.
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) = log2n

You might also like