Uploaded by Vijayakumar R

T9-Searching

advertisement
Winter Camp 2022
C Programming
T9-Searching
Prepared By: REC Faculty 4.0 Team
TOPICS
SEARCHING
LINEAR SEARCH
BINARY SEARCH
MCQ
CODING
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.
SEARCHING
• 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.
LINEAR SEARCH
• 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.
LINEAR SEARCH
• 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.
EFFICIENCY OF LINEAR SEARCH
• 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 preexisting 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.
BINARY SEARCH
• 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.
BINARY SEARCH
• 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)
EFFICIENCY OF BINARY
SEARCH-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.
EFFICIENCY OF BINARY
SEARCH-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.
MCQS
What is the best case for linear
search?
• O(1)
What is the best case for Binary
search?
• O(1)
Given an array arr =
{5,6,77,88,99} and key = 88;
How many iterations are done
until the element is found?
• 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?
• 90 and 99
What is the worst case for linear
search?
• O(n)
What is the worst case for binary
search?
• O(log n)
Programs
Ice Cream Parlor
• Sunny and Johnny like to pool their money and go to
the ice cream parlor. Johnny never buys the same
flavor that Sunny does. The only other rule they have
is that they spend all of their money.
• Given a list of prices for the flavors of ice cream,
select the two that will cost all of the money they
have.
Ice Cream Parlor
• For example, they have m = 6 to spend and there are
flavors costing cost = [1, 2, 3, 4, 5, 6]. The two
flavors costing 1 and 5 meet the criteria. Using 1based indexing, they are at indices 1 and 5.
• Complete the code in the editor below. It should
return an array containing the indices of the prices
of the two flavors they buy, sorted ascending.
Missing Numbers
• Numeros the Artist had two lists that were
permutations of one another. He was very proud.
Unfortunately, while transporting them from one
exhibition to another, some numbers were lost out
of the first list. Can you find the missing numbers?
• As an example, the array with some numbers
missing, arr = [7, 2, 5, 3, 5, 3]. The original array of
numbers brr = [7, 2, 5, 4, 6, 3, 5, 3]. The numbers
missing are [4, 6].
Missing Numbers
• If a number occurs multiple times in the lists, you
must ensure that the frequency of that number in
both lists is the same. If that is not the case, then it is
also a missing number.
• You have to print all the missing numbers in
ascending order. Print each missing number once,
even if it is missing multiple times.
• The difference between maximum and minimum
number in the second list is less than or equal to 100.
Sherlock and Array
• Watson gives Sherlock an array of integers. His challenge is to
find an element of the array such that the sum of all elements
to the left is equal to the sum of all elements to the right. For
instance, given the array arr = [5, 6, 8, 11], 8 is between two
subarrays that sum to 11. If your starting array is [1], that
element satisfies the rule as left and right sum to 0.
• You will be given arrays of integers and must determine
whether there is an element that meets the criterion.
• Complete the code in the editor below. It should return a string,
either YES if there is an element meeting the criterion or NO
otherwise.
Sherlock and Array
Sherlock and Array
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.
Find Peak Element
THANK YOU
Download