Searching Algorithms
Searching Algorithms
Searching
SEARCHING
• Searching refers to determining whether an element is present in a given list of elements or not.
• If the element is found to be present in the list then the search is considered as successful,
otherwise it is considered as an unsuccessful search.
• The search operation returns the location or address of the element found.
• There are various searching methods that can be employed to perform search on a data set.
• The choice of a particular searching method in a given situation depends on a number of factors,
such as:
• Size of the list Let us explore the various searching methods one by one.
TYPES OF SEARCHING
• LINEAR SEARCHING
• BINARY SEARCHING
LINEAR SEARCH
• It is one of the conventional searching techniques that sequentially searches for an element in the
list.
• It typically starts with the first element in the list and moves towards the end in a step-by-step
fashion.
• In each iteration, it compares the element to be searched with the list element, and if there is a
match, the location of the list element is returned.
• The linear search technique will first compare A[0] with k to find out if they are same.
• If the two values are found to be same then the index value, i.e., 0 will be returned as the location
containing k.
• However, if the two values are not same then k will be compared with A[1].
• As shown in Fig., the value k is repeatedly compared with each element of the array A.
• As soon as the element is found, the corresponding index location is returned and the search
operation is terminated.
• In the best case, k would be the first element in the list, thus requiring only one comparison.
• In the worst case, it would be last element in the list, thus requiring n comparisons.
• To compute the efficiency of linear search we can add all the possible number of comparisons and
divide it by n.
= (1 + 2 + . . . + n) / n
= n(n + 1) / 2n
= O(n)
• It does not leverage the presence of any pre-existing sort order in a list.
BINARY SEARCH
• Binary search technique has a prerequisite – it requires the elements of a data structure (list) to be
• It begins by comparing the element that is present at the middle of the list.
• If there is a match, then the search ends immediately and the location of the middle element is
returned.
• However, if there is a mismatch then it focuses the search either in the left or the right sub list
depending on whether the target element is lesser than or greater than middle element.
• The same methodology is repeatedly followed until the target element is found.
• Consider an array of integers A containing eight elements, as shown in Fig. Let k = 21 be the value
• As we can see in Fig., the array A on which binary search is to be performed is already sorted.
• The following steps describe how binary search is performed on array A to search for value k:
• First of all, the middle element in the array A is identified, which is 18.
• Now, k is compared with 18. Since k is greater than 18, the search is focused on the right sub list.
• The middle element in the right sub list is 33. Since k is less than 33, the search is focused on the
left sub list, which is {21, 33}.
• Now, again k is compared with the middle element of {21, 33}, which is 21. Thus, it matches with K
• The index value of 21, i.e., 4 is returned and the search is considered as successful.
EFFICIENCY OF BINARY SEARCH
-BEST CASE
• The best case for a binary search algorithm occurs when the element to be searched is
• In this case, only one comparison is made to perform the search operation.
-WORST CASE
• The worst case for a binary search algorithm occurs when the element to be
• Let n be the number of list elements and c be the total number of comparisons made in
• Now, after every single comparison, the number of list elements left to be searched is reduced by
2.
• Thus, c = logn
• Unlike linear search, it requires the list to be sorted before search can be performed.
• In comparison to linear search, the binary search technique may seem to be a little
difficult to implement.
MCQ
Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done until the element is
found?
Ans: 2
What are the mid values (corresponding array elements) generated in the first and second iterations?
• Output: 2
• Explanation: 3 is a peak element and your function should return the index number 2.