0% found this document useful (0 votes)
1 views

Searching Algorithms

The document discusses searching techniques, focusing on linear and binary search methods. Linear search sequentially checks each element in a list, while binary search requires a sorted list and divides the search space in half with each comparison. The efficiency, advantages, and limitations of both methods are also outlined, along with examples and a brief mention of peak elements.

Uploaded by

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

Searching Algorithms

The document discusses searching techniques, focusing on linear and binary search methods. Linear search sequentially checks each element in a list, while binary search requires a sorted list and divides the search space in half with each comparison. The efficiency, advantages, and limitations of both methods are also outlined, along with examples and a brief mention of peak elements.

Uploaded by

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

Week 7

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:

• Order of elements in the list, i.e., random or sorted.

• 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.

 Consider an array of integers A containing n elements.

• Let k be the value that needs to be searched.

• 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].

• This process will be repeated until the element is not found.


• If the last comparison between k and A[n– 1] is also negative then the search will be considered as
unsuccessful.

• Figure depicts the linear search technique performed on an array of integers.

• 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.

EFFICIENCY OF LINEAR SEARCH

• Assume that an array containing n elements is to be searched for the value k.

• 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.

• Thus, efficiency of linear search

= (1 + 2 + . . . + n) / n

= n(n + 1) / 2n

= O(n)

ADVANTAGES OF LINEAR SEARCH


• It is a simple searching technique that is easy to implement.

• It does not require the list to be sorted in a particular order.

LIMITATIONS OF LINEAR SEARCH

• It is quite inefficient for large sized lists.

• 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

already arranged in a sorted manner before search can be performed in it.

• 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

that needs to be searched.

• 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

present at the middle of the list.

• In this case, only one comparison is made to perform the search operation.

• Thus, efficiency = O(1)

-WORST CASE

• The worst case for a binary search algorithm occurs when the element to be

searched is not present in the list.

• Let n be the number of list elements and c be the total number of comparisons made in

the worst case.

• Now, after every single comparison, the number of list elements left to be searched is reduced by
2.

• Thus, c = logn

• Hence, efficiency = O(logn)

ADVANTAGES OF BINARY SEARCH


• It requires lesser number of iterations.

• It is a lot faster than linear search.

LIMITATIONS OF BINARY SEARCH

• 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

Given an array arr = {45,77,89,90,94,99,100} and key = 100;

What are the mid values (corresponding array elements) generated in the first and second iterations?

Ans: 90, 99, 100

Find Peak Element

• A peak element is an element that is strictly greater than its neighbours.

• Input: nums = [1,2,3,1]

• Output: 2

• Explanation: 3 is a peak element and your function should return the index number 2.

You might also like